Category Archives: SharePoint Development

Using The ULS In CSOM / Office 365

SharePoint online has really limited availability for error logging built in. Most of the time you have to throw the exception to either standard output, a list, or if it is a provider hosted application using a local text file. None of these options is terrible attractive since SharePoint already has logging availability in the ULS and a lot of fancy tools that allow relevant parsing to occur.

Using the ULS off premise isn’t really as easier as it is on premise (shocker). To do it, firstly, you have to make the relevant service reference. This is located here:

http://{site collection}/_vti_bin/Diagnostics.asmx

Once you have that, if you are deploying a generic service application remember to go to your settings page and add some logic in order to look up the current URL using a dynamic reference. If not, then you are almost there, it is helpful to push this logic to some sort of static method if you are using it for broad scope exception handling, but the meat is in the SendClientScriptErrorReport method. You can find this off the diagnostic object, this will depend on how you qualify it however (i.e. a top level alias directive). The method itself will take the form:

[csharp]

proxy.SendClientScriptErrorReport(“message”, “filename”, “line number”, “client”, “stack”, “team”, “filename);

[/csharp]

Share

Add An Item To A Folder To A List With CSOM

Its really similar to how you would do it with traditional server side OM means. This is a static helper method that will generate a new folder in some list. Essentially we just have to spin a ListItemCreationInformation object specified with empty properties and just set the FolderUrl.

[csharp]

public static ListItem AddItemInFolder(string siteUrl, string listName, string folderPath, ClientContext clientContext)
{
List list = clientContext.Web.Lists.GetByTitle(listName);
ListItemCreationInformation listItemCreationInformation = null;
if (!String.IsNullOrEmpty(folderPath))
{
listItemCreationInformation = new ListItemCreationInformation();
listItemCreationInformation.FolderUrl = String.Format(“{0}/{1}”, siteUrl, folderPath);
}
ListItem listItem = list.AddItem(listItemCreationInformation);
listItem.Update();
clientContext.ExecuteQuery();
return listItem;
}

[/csharp]

Share

Getting Safe Value From ListItem In Office 365 And SharePoint Online

Reading a ListItem in SharePoint online is nothing fantastic and is pretty similar to reading list items in the throwback server OM models. Consider the below example that starts with a ListItemCollection. We are reading the value of SomeCrap. The problem here is, what if that returns null? That’s an issue.

[csharp]

ListItemCollection items = list.GetItems(CamlQuery.CreateAllItemsQuery());
list.Context.Load(items);
list.Context.ExecuteQuery();
foreach (ListItem item in items)
{
list.Context.Load(item, x => x[“SomeCrap”]);
list.Context.ExecuteQuery();
string someCrap = item[“SomeCrap”].ToString();
}

[/csharp]

There is an easy extension means to handle this though, where instead of null due to the ToString() it will just give the default string.Empty. Here is the extension and an example of its use.

[csharp]

public static T GetSafeValue(this ListItem item, string fieldName) where T : class
{
object o = item[fieldName];
if (o == null || !(o is T))
{
return null;
}
return (T)o;
}

then its use.

[csharp]

foreach (ListItem listItem in items)
{
string someCrap = listItem.GetSafeValue(“SomeCrap”);
}

[/csharp]

Share