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.

 

Tuesday, October 8, 2013

jMeter 2.9 & SharePoint 2010

Intro:
So I was tasked to find the cheapest way to apply load onto a SharePoint environment. Found jMeter which lots of people raved about.

My sample SharePoint environment is:
Windows 2012 Standard
SharePoint 2013
JMeter 2.9

Symptoms/Issue:
So I created a simple load test to hit the default page of my site collection but all I got were 401s.
Response code: 401
Response message: Unauthorized
HTTP/1.1 401 Unauthorized

How to create a basic page request in JMeter for SharePoint:
  1. Download Java Runtime Environment: http://www.java.com/en/download/index.jsp
  2. Download JMeter: https://jmeter.apache.org/download_jmeter.cgi
  3. Extract JMeter.
  4. Run jmeter.bat from the bin folder.
  5. Right click Test Plan > Add > Threads (User) > Thread Group
  6. Right click on "Thread Group" > Add > Sampler > HTTP Request
  7. In the new HTTP Request you just added:
  8. Server Name or IP: uri of your site collection (without the http(s)). For example if the landing page of my site collection is: http://spdev/Pages/Default.aspx. You would just have spdev in this field.
  9. Port Number: can be left blank if using port 80.
  10. Implementation: Java <- this is the step I had to change to get this whole thing to work for me.
  11. Path: in this example I put /Pages/Default.aspx
  12. Right click on "Thread Group" > Add > View Results in Table (or View Results Tree).
  13. Now click: Run > Start (or Ctrl + R)


 

Sunday, October 6, 2013

SharePoint 2013: CSOM List Permissions in C#

Intro: I'm building a SharePoint 2013 provided hosted app. I had a requirement to retrieve permissions for the current user to a particular list. I want to enable/disable a checkbox depending on their ability to add/edit items in a particular SharePoint list. So I search around and find this:
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.list.effectivebasepermissions.aspx
Unfortunately there's no sample in the above article and aren't any examples of how to use it anywhere on the web.

Issue: The EffectiveBasePermissions isn't loaded unless you specifically ask for it. This is the CSOM way.

Solution:
using (var clientContext = new ClientContext(hostWeb))
{
    Site site = clientContext.Site;
    clientContext.Load(site);
    clientContext.ExecuteQuery();

    Web web = site.OpenWeb("Name of spweb");
    clientContext.Load(web);
    clientContext.ExecuteQuery();

    List list = web.Lists.GetByTitle("Name of List");
    clientContext.Load(list, l => l.EffectiveBasePermissions); 
    clientContext.ExecuteQuery();

    bool myAddPermission = list.EffectiveBasePermissions.Has(PermissionKind.AddListItems);
    bool myEditPermission = list.EffectiveBasePermissions.Has(PermissionKind.EditListItems);
}

The code above will more than likely get the permission of the IIS App pool running your app. You may need to change the credentials in which the clientContext is running:
clientContext.Credentials = new NetworkCredential();

Explanation:
When writing server side code we can get all the data for the Web or list. Because we can afford to. But when we use CSOM we want to minimise the amount of data going back and forth. So the clientContext won't have the "EffectiveBasePermissions" unless you tell it to.

Wednesday, July 24, 2013

BigPond Usage Meter

Ok so this isn't quite SharePoint related. But for those in Australia using Bigpond. Here's a great little Windows gadget which displays your usage:
http://www.gm2k.net/gadgets/bpusagemeter/

I'm using it to view my 100mbps cable connection usage.

Loving cable.