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

Getting The Full, Absolute URL Of A ListItem In Office 365

I don't know why Microsoft made it such a pain in the ass to get the url for most of the proxy objects on the SharePoint server object model, but in Office 365 it is even worse. Here is an example of how to get a ListItem actual URL and the modified date of the item. Following I will throw the values into a dictionary.

[csharp]

Dictionary urlDict = new Dictionary();
foreach (List list in ListsToProcess)
{
ListItemCollection items = list.GetItems(CamlQuery.CreateAllItemsQuery());
list.Context.Load(items);
list.Context.ExecuteQuery();
foreach (SharePointListItem item in items)
{
list.Context.Load(item, x => x["FileRef"], x => x["Modified"]);
string startUrl = list.Context.Url;
list.Context.ExecuteQuery();
string fullUrl = startUrl + item["FileRef"];
string lastModified = item["Modified"].ToString();
if (!urlDict.ContainsKey(fullUrl))
{
urlDict.Add(fullUrl, lastModified);
}
}
}

[/csharp]

Share