Thursday, 13 November 2014

Update SharePoint List Item using CSOM

public void AddChangeRequestFields(string strDocToRefer, string strUserFQN, string strMode)
{
SPSite siteCollection = null;
SPWeb topLevelSite = null;
string[] arrXRefDocs;
string strNewXRef = String.Empty;
siteCollection = new SPSite(System.Configuration.ConfigurationManager.AppSettings["SPDocumentLibrary"].ToString());
topLevelSite = siteCollection.AllWebs[ConfigurationSettings.AppSettings["TopLevelSite"].ToString()];
topLevelSite.AllowUnsafeUpdates = true;
SPList list = topLevelSite.Lists["Approved Documents"];
foreach (SPListItem item in list.Items)
{
if (strDocToRefer.Equals(Convert.ToString(item["Document Number"])))
{
if (strMode.Equals("Add"))
{
item["Initiate Change Request"] = "";
item["CR Initiated By"] = strUserFQN;
}
else
{
item["Initiate Change Request"] = System.Configuration.ConfigurationManager.AppSettings["SPDocumentLibrary"].ToString() + "?ID=" + strDocToRefer.Trim() + "&Type=CR";
item["CR Initiated By"] = strUserFQN;
}
item.Update();
break;
}
}
topLevelSite.AllowUnsafeUpdates = false;
siteCollection.Dispose();
topLevelSite.Dispose();
}
view raw gistfile1.cs hosted with ❤ by GitHub