Update Exclusive properties[AllowMultiResponses & ShowUser] of a Survey List for SharePoint using service and CSOM C#

Survey List for SharePoint is like any other List except that its got some exclusive properties:

  • ShowUser – Indicates whether to display the user’s name along side its response. Default value is True.
  • AllowMultiResponses – Indicates whether an user can post multiple response for a Survey. Default value is False.

Now usually to update other common properties of a List, like Title, etc., we can easily rely on CSOM. However, there’s no such provision for these two properties or any other exclusive properties of any other List. Here, we’ll be focussing on the Survey List only.So when the Client Object Model fails, turn to services. The service Lists.asmx, comprises of many methods related to Lists. One of the method is UpdateList. This method has the following definition:

[SoapDocumentMethodAttribute("http://schemas.microsoft.com/sharepoint/soap/UpdateList", RequestNamespace="http://schemas.microsoft.com/sharepoint/soap/", ResponseNamespace="http://schemas.microsoft.com/sharepoint/soap/", Use=SoapBindingUse.Literal, ParameterStyle=SoapParameterStyle.Wrapped)] 
public XmlNode UpdateList (
    string listName,
    XmlNode listProperties,
    XmlNode newFields,
    XmlNode updateFields,
    XmlNode deleteFields,
    string listVersion
)

The XmlNode parameter listProperties, is the one I’ll be focusing at. This will be in the format of a

XmlNode where we can update/modify the following properties:
AllowMultiResponses TRUE to allow multiple responses to the survey.
Description A string that contains the description for the list.
Direction A string that contains LTR if the reading order is left-to-right, RTL if it is right-to-left, or None.
EnableAssignedToEmail TRUE to enable assigned-to e-mail for the issues list.
EnableAttachments TRUE to enable attachments to items in the list. Does not apply to document libraries.
EnableModeration TRUE to enable Content Approval for the list.
EnableVersioning TRUE to enable versioning for the list.
Hidden TRUE to hide the list so that it does not appear on the Documents and Lists page, Quick Launch bar, Modify Site Content page, or Add Column page as an option for lookup fields.
MultipleDataList TRUE to specify that the list in a Meeting Workspace site contains data for multiple meeting instances within the site.
Ordered TRUE to specify that the option to allow users to reorder items in the list is available on the Edit View page for the list.
ShowUser TRUE to specify that names of users are shown in the results of the survey.
Title A string that contains the title of the list.

As you can see the two exclusive properties of the List Survey, ShowUser & AllowMultiResponses are present here which is exactly what we need. The other properties can also be updated using the CSOM so I won’t be using them here.

One important thing, this method simultaneously, also creates, updates, & deletes Fields/SiteColumns for the given List. However, if you don’t want to use them (as will be the case here) you can pass an empty string as parameters.

Following is the code sample to accomplish this task. The first part demonstrates how to construct the parameter, listProperties.

Desired Format
<List Title=”List_Name” Description=”List_Description” Direction=”LTR”/>

XmlDocument xmlDoc = new System.Xml.XmlDocument();
XmlNode ndProperties = xmlDoc.CreateNode(XmlNodeType.Element, "List", "");
XmlAttribute ndTitleAttrib = (XmlAttribute)xmlDoc.CreateNode(XmlNodeType.Attribute, "Title", "");
XmlAttribute ndDescriptionAttrib = (XmlAttribute)xmlDoc.CreateNode(XmlNodeType.Attribute, "Description", "");
XmlAttribute ndDirectionAttrib = (XmlAttribute)xmlDoc.CreateNode(XmlNodeType.Attribute, "Direction", "");
XmlAttribute ndMultiresponse = (XmlAttribute)xmlDoc.CreateNode(XmlNodeType.Attribute, "AllowMultiResponses", "");
XmlAttribute ndShowUser = (XmlAttribute)xmlDoc.CreateNode(XmlNodeType.Attribute, "ShowUser", "");

ndTitleAttrib.Value = "My Survey";
ndDescriptionAttrib.Value = "Allowing multiple responses for this Survey";
ndDirectionAttrib.Value = "LTR";
ndMultiresponse.Value = "TRUE";
ndShowUser.Value = "TRUE";

ndProperties.Attributes.Append(ndTitleAttrib);
ndProperties.Attributes.Append(ndDescriptionAttrib);
ndProperties.Attributes.Append(ndDirectionAttrib);
ndProperties.Attributes.Append(ndMultiresponse);
ndProperties.Attributes.Append(ndShowUser);

So basically we're creating the following node

<
List Title="My Survey" Description="Allowing multiple responses for this Survey" Direction="LTR" AllowMultiResponses="TRUE" ShowUser="TRUE"/>

Finally, we’re going to use this parameter in the UpdateList method. The following code is a sample demonstration to call this method from the Lists.asmx service. Note that since this service is dependant on the site’s url, it will always vary from site to site. Hence we’re setting the url of the web service at runtime. One more thing, all the parameters that will be passed should be string i.e., the OuterXml property for XmlNode parameters.

string webServiceUrl = ctx.Web.Url + "/_vti_bin/Lists.asmx";

StringBuilder sbEnvelope = new StringBuilder();
sbEnvelope.Append("");
sbEnvelope.Append("");
sbEnvelope.Append(String.Format(
    "" +
        "" +
            "{0}" +
            "{1}" +
            "{2}" +
            "{3}" +
            "{4}" +
            "{5}" +
        "" +
    ""
    , id, ndProperties.OuterXml, String.Empty, String.Empty, String.Empty, version));
sbEnvelope.Append("");

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(webServiceUrl);
req.Method = "POST";
req.ContentType = "text/xml; charset=\"utf-8\"";
req.Accept = "text/xml";
req.Headers.Add("SOAPAction", "\"http://schemas.microsoft.com/sharepoint/soap/UpdateList\"");
req.UserAgent = "FrontPage";
req.UseDefaultCredentials = false;

Uri targetSite = new Uri(ctx.Web.Url);
SharePointOnlineCredentials spCredentials = (SharePointOnlineCredentials)ctx.Credentials;
string authCookieValue = spCredentials.GetAuthenticationCookie(targetSite);
req.CookieContainer = new CookieContainer();
req.CookieContainer.Add(
    new Cookie("FedAuth",
        authCookieValue.TrimStart("SPOIDCRL=".ToCharArray()),
        String.Empty,
        targetSite.Authority));

using (IO.Stream stream = req.GetRequestStream())
{
    using (IO.StreamWriter writer = new IO.StreamWriter(stream))
    {
        writer.Write(sbEnvelope.ToString());
    }
}

WebResponse response = req.GetResponse();
using (IO.Stream responseStream = response.GetResponseStream())
{
    XmlDocument xDoc = new XmlDocument ();
    xDoc.Load(responseStream);

    if (xDoc.DocumentElement != null && xDoc.DocumentElement.InnerText.Length > 0)
    {
        Debug.WriteLine(String.Concat(DateTime.Now.ToShortTimeString(), " Response of the Survey List Update: ", xDoc.DocumentElement.InnerText));
    }
}

Here’s a screen-shot of the UpdateList method. You can view your site’s service at your site’s url + “_vti_bin/Lists.asmx”

Get all the WebParts of a SharePoint WebPage using Client Object Model C#

CSOM doesn’t have much to do with WebParts. However, you can modify certain properties like, Title. Below, I have demonstrated a simple way to get the WebParts of the page, Home, from the List, SitePages, of a TeamSite.
ClientContext ctx = new ClientContext(weburl);
ctx.Credentials = new SharePointOnlineCredentials(userName, passWord);

SP.List list = ctx.Web.Lists.GetByTitle("Site Pages");
ctx.Load(list);
CamlQuery cQuery = new CamlQuery();
ListItemCollection ltItemCollection = list.GetItems(cQuery);

ctx.Load(ltItemCollection);
ctx.ExecuteQuery();

ListItem ltItemHome = ltItemCollection.FirstOrDefault(p => p.DisplayName == "Home");

LimitedWebPartManager lwpmShared = ltItemHome.File.GetLimitedWebPartManager(PersonalizationScope.Shared);
LimitedWebPartManager lwpmUser = ltItemHome.File.GetLimitedWebPartManager(PersonalizationScope.User);

WebPartDefinitionCollection webPartDefinitionCollectionShared = lwpmShared.WebParts;
WebPartDefinitionCollection webPartDefinitionCollectionUser = lwpmUser.WebParts;

ctx.Load(webPartDefinitionCollectionShared, w => w.Include(wp => wp.WebPart, wp => wp.Id));
ctx.Load(webPartDefinitionCollectionUser, w => w.Include(wp => wp.WebPart, wp => wp.Id));
ctx.Load(ltItemHome.File);
ctx.Load(ctx.Web, p => p.Url);
ctx.ExecuteQuery();
 
foreach (WebPartDefinition webPartDefinition in webPartDefinitionCollectionShared)
{
    WebPart webPart = webPartDefinition.WebPart;
    ctx.Load(webPart, wp => wp.ZoneIndex, wp => wp.Properties, wp => wp.Title, wp => wp.Subtitle, wp => wp.TitleUrl);
    ctx.ExecuteQuery();
    
    //Once the webPart is loaded, you can do your modification as follows
    webPart.Title = "My New Web Part Title";
    webPartDefinition.SaveWebPartChanges();
    ctx.ExecuteQuery();
}

To get the SchemaXml of a particular WebPart of a SharePoint Page using Web Services, visit the following link,
https://realmpksharepoint.wordpress.com/2014/04/03/get-the-schemaxml-of-a-particular-webpart-of-a-sharepoint-page-using-web-services-c/

Update a UserMulti Column value in Client Object Model C#

Update the value of User/UserMulti column in SharePoint using Client Object Model
ClientContext ctx = new ClientContext(weburl);
ctx.Credentials = new SharePointOnlineCredentials(userName, passWord); 

List list = ctx.Web.Lists.GetByTitle("ListTitle");
ListItem currentItem = list.GetItemById(1);

FieldUserValue[] userValueCollection = new FieldUserValue[1];

//Get all the users of this Web
UserCollection userCollection = this.ctx.Web.SiteUsers; 
this.ctx.Load(userCollection, w => w.Include(p => p.Id, p => p.Title));
this.ctx.ExecuteQuery(this.ctx);

User user = userCollection.FirstOrDefault(p => p.Title.ToLower().Trim() == userTitle.ToLower().Trim());

//Making sure that a user of title userTitle is present in this Web
if (user != null)
{
    FieldUserValue fieldUserVal = new FieldUserValue();
    fieldUserVal.LookupId = user.Id;
    userValueCollection.SetValue(fieldUserVal, 0);
}

currentItem["MultiUserValCol"] = userValueCollection;
currentItem.Update(); 
this.ctx.ExecuteQuery(); 

As you can see that updating a multiUser column is along the same line as updating a LookupMulti column. Hence, there are a couple of things that one should take care of while updating this column. Those are mentioned in my previous blog where I have discussed about updating a LookupMulti column. here. https://realmpksharepoint.wordpress.com/2014/01/19/update-a-lookupmulti-column-value-in-sharepoint-using-client-object-model-c/

Update a LookupMulti Column value in SharePoint using Client Object Model C#

Update the value of LookupMulti column in SharePoint using the Client Object Model
ClientContext ctx = new ClientContext(weburl);
ctx.Credentials = new SharePointOnlineCredentials(userName, passWord); 

List list = ctx.Web.Lists.GetByTitle("ListTitle");
ListItem currentItem = list.GetItemById(1);

FieldLookupValue[] lookupFieldValCollection = new FieldLookupValue[3];FieldLookupValue lookupFieldA = new FieldLookupValue();
lookupFieldA.LookupId = 2;
lookupFieldValCollection.SetValue(lookupFieldA, 0);
FieldLookupValue lookupFieldB = new FieldLookupValue();
lookupFieldB.LookupId = 4;
lookupFieldValCollection.SetValue(lookupFieldB, 1);
FieldLookupValue lookupFieldC = new FieldLookupValue();
lookupFieldC.LookupId = 6;
lookupFieldValCollection.SetValue(lookupFieldC, 2);

currentItem["MultiLookupValCol"] = lookupFieldValCollection;
currentItem.Update();

ctx.ExecuteQuery();  

Two important thing I would like to share here,

  • Initially I tried to pass an array of an array of integer [containing the ids of the item] to the LookupMulti column which generated the error: Value does not fall between the specified range
  • Another thing was then I created a List of type FieldLookupValue and then I passed the List to the column using its ToArray(). I guess we just had to pass the array of FieldLookupValue. However, this resulted in the error: Unknown Error. Don’t know what that means.

Finally, I can say that create the array of type FieldLookupValue and populate this array using its SetValue method only.

Update a LookupField value for a SharePoint ListItem using Client Object Model C#

A lookup data in a SharePoint can be treated as a choice options that has been maintained in a different List. Hence if one of your field in your List is a lookup then, its value should be the ID[integer] the actual Lookup-Item (i.e., the list item id of the List as a LookUp).

For ex, in a blog site, List, Category has been used as lookup field for the List Post. By default, in SP2013, you’ll get three items for Categories. Say, their Ids are 1,2, & 3.

  • Events[1]
  • Ideas[2]
  • Opinions[3]
Now if you want to change the Category field of a Post, then, you have to pass the ID of the item and not the text value.

ClientContext ctx = new ClientContext(targetURL);
ctx.Credentials = new SharePointOnlineCredentials(userName, passWord);

this.ctx.Load(this.ctx.Web);
SP.ListCollection listCol = web.Lists;
ctx.Load(listCol);

SP.List list = listCollection.GetByTitle("ListTitle");

SP.CamlQuery camlQuery = new SP.CamlQuery();
camlQuery.ViewXml = "";
SP.ListItemCollection listItemCollection = list.GetItems(camlQuery);
this.ctx.Load(listItemCollection);
this.ctx.ExecuteQuery();

SP.ListItem listItem = listItemCollection.FirstOrDefault(t => t.DisplayName == "NameOfTheListItem");
this.ctx.Load(listItem.FieldValuesAsText); 
this.ctx.ExecuteQuery();

//[Say, for one of the Id mentioned above.]
listItem.FieldValuesAsText["PostCategory"] = 2; 

listItem.Update(); 
this.ctx.ExecuteQuery(); 

To get the id of the listItem Ideas, you’ll have to re-connect to the SharePoint server and get the ID for the text Ideas from the List Category.

NOTE: Here, I have faced a strange issue. If I update any value of the Post Item before updating the lookupField value then the previous made changes were getting overwritten. So I have to ensure that the lookupField value should always get updated first before any other field.

Create, Update, Delete a SharePoint List and add a new Field using Client Object Model

Create and update a SharePoint list

// Starting with ClientContext, the constructor requires a URL to the 
// server running SharePoint. 
ClientContext context = new ClientContext("http://SiteUrl"); 

// The SharePoint web at the URL.
Web web = context.Web; 

ListCreationInformation creationInfo = new ListCreationInformation(); 
creationInfo.Title = "My List"; 
creationInfo.TemplateType = (int)ListTemplateType.Announcements; 
List list = web.Lists.Add(creationInfo); 
list.Description = "New Description"; 

list.Update(); 
context.ExecuteQuery(); 

Delete a SharePoint list

// Starting with ClientContext, the constructor requires a URL to the 
// server running SharePoint. 
ClientContext context = new ClientContext("http://SiteUrl"); 

// The SharePoint web at the URL.
Web web = context.Web; 

List list = web.Lists.GetByTitle("My List"); 
list.DeleteObject(); 

context.ExecuteQuery();  

Add a field to a SharePoint list

This example adds a field to a SharePoint list. add an alias to the using statement for Microsoft.SharePoint.Client namespace so you can refer to its classes unambiguously. For example,

using SP = Microsoft.SharePoint.Client;
// Starting with ClientContext, the constructor requires a URL to the 
// server running SharePoint. 
ClientContext context = new ClientContext("http://SiteUrl"); 

SP.List list = context.Web.Lists.GetByTitle("Announcements"); 

SP.Field field = list.Fields.AddFieldAsXml("", 
 true, 
 AddFieldOptions.DefaultValue); 

SP.FieldNumber fldNumber = context.CastTo<FieldNumber>(field); 
fldNumber.MaximumValue = 100; 
fldNumber.MinimumValue = 35; 
fldNumber.Update(); 

context.ExecuteQuery(); 

Create, Update, Delete a List Item in SharePoint 2013 by Client Object Model

Create List Item:

Add the references Microsoft.SharePoint.dll and Microsoft.SharePoint.Client.dll.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Client;
namespace CreateListItem
{
    class Program
    {
        static void Main(string[] args)
        {
            string siteUrl = "http://servername:7777/";
            ClientContext clientContext = new ClientContext(siteUrl);
            List oList = clientContext.Web.Lists.GetByTitle("ListTitle");
            ListItemCreationInformation listCreationInformation = new ListItemCreationInformation();
            ListItem oListItem = oList.AddItem(listCreationInformation);
            oListItem["Title"] = "Hello World";
            oListItem.Update();
            clientContext.ExecuteQuery();
        }
    }
}

Update List Item:

Add the references Microsoft.SharePoint.dll and Microsoft.SharePoint.Client.dll.

using System;
using System.Collections.Generic; 
using System.Linq;
using System.Text; 
using Microsoft.SharePoint;
using Microsoft.SharePoint.Client;

namespace UpdateListItem
{
    class Program
    {
        static void Main(string[] args)
        {
            string siteUrl = "http://servername:7777/";
            ClientContext clientContext = new ClientContext(siteUrl);
            List oList = clientContext.Web.Lists.GetByTitle("ListTitle");
            ListItem oListItem = oList.GetItemById(1);
            oListItem["Title"] = "Hello World Updated.";
            oListItem.Update();
            clientContext.ExecuteQuery();
        }
    }
}

Delete List Item:

Add the references Microsoft.SharePoint.dll and Microsoft.SharePoint.Client.dll.
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Microsoft.SharePoint; 
using Microsoft.SharePoint.Client;

namespace UpdateListItem
{
    class Program
    {
        static void Main(string[] args)
        {
            string siteUrl = "http://servername:7777/";
            ClientContext clientContext = new ClientContext(siteUrl);
            List oList = clientContext.Web.Lists.GetByTitle("ListTitle");
            ListItem oListItem = oList.GetItemById(1);         
            oListItem.DeleteObject();
            clientContext.ExecuteQuery();
        }
    }
}