1

I am working on a ASP.NET 4.5 C# MVC 5 project that integrates with a SharePoint site using Microsoft.Sharepoint.Client. One of the methods must add a SQL record, and thereafter use 2 of that record's fields, assign them to the Title field (created by default by SharePoint) and the other to a numeric PartnerID field to create an item to be added to a SharePoint list.

The SQL portion executes successfully but when adding to SharePoint, the ID is set correctly and the Title value is emptied. This is despite the Title value being the correct size and matching the single line of text data type. Please see the snippet below and advise what I could be doing wrong.

List listCPD = apiContext.Web.Lists.GetByTitle("CommunityPartner");
ListItemCreationInformation creationItem = new ListItemCreationInformation();
ListItem lstItem = listCPD.AddItem(creationItem);

lstItem["Title"] = string.IsNullOrEmpty(cPAModel.CommunityPartner) || string.IsNullOrWhiteSpace(cPAModel.CommunityPartner) ? "Title not specified" : cPAModel.CommunityPartner; //check for empty string to ensure no empty string from user input level
lstItem["PartnerID"] = cPAModel.CommunityPartnerID;

lstItem.Update();
apiContext.ExecuteQuery();
0

Tested the snippet below in Visual Studio:

            var ctx = new ClientContext(siteUrl);
            List olist = ctx.Web.Lists.GetByTitle("MyList22");
            ListItemCreationInformation creationItem = new ListItemCreationInformation();
            ListItem lstItem = olist.AddItem(creationItem);
            var teststring = "";
            lstItem["Title"] = string.IsNullOrEmpty(teststring) || string.IsNullOrWhiteSpace(teststring) ? "Title not specified" : "Test"; 
            lstItem.Update();
            ctx.ExecuteQuery();

The Title field was set to "Title not specified" without problem:

enter image description here enter image description here

| improve this answer | |
  • Thanks for the response @Jerry_MSFT. At code level, I also get the value of Title when debugging but it seems it gets cleared out after either lstItem.Update() or ctx.ExecuteQuery() is called. – Thobanjalo Shangase Jan 20 at 9:57
  • Also, if I only assign the Title value the title is not emptied. If I set Title and an additional field/s, only the last field that I set is not emptied – Thobanjalo Shangase Jan 20 at 10:01
  • @ThobanjaloShangase, if Title field can be set with a assigned value, it shoulen't be a issue to update with another field. I suggest you could create a new empty list in SharePoint to see if you can reproduce the issue. – Jerry_MSFT Jan 20 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.