Wednesday, November 20, 2013

SharePoint field + XML = Blank field

Intro:
I've been tasked with storing the contents of an RSS feed (xml) to SharePoint via a Custom Timer Job.

When I tried to run the following code. I simply got a blank field for the "RSSContents" field of the new item.

using (System.Net.WebClient client = new System.Net.WebClient())
{
using (Stream data = client.OpenRead("http://www.bom.gov.au/fwo/IDZ00059.warnings_vic.xml"))
{
using (StreamReader reader = new StreamReader(data))
{
    SPWebApplication webApp = this.Parent as SPWebApplication;
    SPList list = webApp.Sites[0].RootWeb.Lists["TimerTest"];
    SPListItem newListItem = list.Items.Add();
    newListItem["Title"] = DateTime.Now.ToString();
    string s = reader.ReadToEnd();
    newListItem["RSSContents"] = s;
    newListItem.Update();
}
}
}

Workaround:
Add HtmlEncode into the mix:
using (System.Net.WebClient client = new System.Net.WebClient())
{
using (Stream data = client.OpenRead("http://www.bom.gov.au/fwo/IDZ00059.warnings_vic.xml"))
{
using (StreamReader reader = new StreamReader(data))
{
    SPWebApplication webApp = this.Parent as SPWebApplication;
    SPList list = webApp.Sites[0].RootWeb.Lists["TimerTest"];
    SPListItem newListItem = list.Items.Add();
    newListItem["Title"] = DateTime.Now.ToString();
    string s = reader.ReadToEnd();
    s = WebUtility.HtmlEncode(s);
    newListItem["RSSContents"] = s;
    newListItem.Update();
}
}
}

I'm working in a SharePoint 2013 environment. So your results may vary. But I suspect this is the same across each of the different SharePoint versions. 

How to: Debug a timer job deployment?

Intro:
So I've been tasked with writing a Custom SharePoint timer job.
So I googled it up and I followed Andrew Connell's article
http://www.andrewconnell.com/Creating-Custom-SharePoint-Timer-Jobs

Symptoms:
After I made modifications to suit my needs I go and try to deploy it (by pressing F5). After which I get a very descriptive message:
Error occurred in deployment step 'Activate Features': Object reference not set to an instance of an object.
So I comment out every line of the "Activate Features" code and try to diagnose it. Breakpoints don't seem to work.

Workaround/Resolution:
Insert
System.Diagnostics.Debugger.Launch();
into your code and if all goes right Visual Studio will pick up the task of debugging your code.

SharePoint 2013: RemoteAppUrl is not specified.

Situation: I've built a Provider Hosted SharePoint 2013 app. It's been working happily until I went to "clean up the project". I made a couple of changes to the IIS Web App project properties and now when I try to deploy my app to SharePoint I get "RemoteAppUrl is not specified."
But it doesn't provide any clues as to how this can be fixed.

Fix:
In Solution Explorer.
Right click on the Web App and click Properties.
Open up the "Web" section.
Under Servers check "Apply server settings to all users (store in project file)".

Why?:
I believe if you have this unchecked Visual Studio won't set the "RemoteAppUrl" in the AppManifest.xml and thus the message.