none
Creating SharePoint online Survey list programmatically and set properties "AllowMultipleResponses" RRS feed

  • Question

  • Hi,

    Is there a way to create survey app and set its property "AllowMultipleResponses" to true in SharePoint Online using CSOM or REST?

    Regards,

    Palak Jain

    Friday, April 21, 2017 11:50 AM

All replies

  • As you are using SharePoint online, you can find lot of free app in Market place. For example: Helpdesk result and smart poll etc..

    ----------------------------------------------------------------------------

    Thanks

    Lalith

    ----------------------------------------

    Please mark as Answer, if this post solves your issue.


    Friday, April 21, 2017 12:24 PM
  • Hi Lalith,

    Thanks for the response. But, I want to know how to do it programmatically.

    Regards,

    Palak Jain

    Friday, April 21, 2017 1:04 PM
  • Implement custom survey using CSOM or REST is major task. You need to identify requirement, Design pages, Business condition etc.. I am pretty much sure that, we can accomplish. But implementation is nothing but small project.

    Thanks

    --------------

    Lalith

    Friday, April 21, 2017 3:50 PM
  • Hi Palak,

    To create a survey list, we can use the following code snippet for achieve it.

    string targetSiteURL = @"https://dennis.sharepoint.com/sites/dennis";
    
    var login = "dennis@dennis.onmicrosoft.com";
    var password = "******";
    
    var securePassword = new SecureString();
    
    foreach (char c in password)
    {
    	securePassword.AppendChar(c);
    }
    SharePointOnlineCredentials onlineCredentials = new SharePointOnlineCredentials(login, securePassword);
    
    ClientContext ctx = new ClientContext(targetSiteURL);
    ctx.Credentials = onlineCredentials;
    
    ListCreationInformation lci = new ListCreationInformation();
    lci.Description = "Custom Survey";
    lci.Title = "First Survey";
    lci.TemplateType = (int)ListTemplateType.Survey;
    
    List newSurveyList = ctx.Web.Lists.Add(lci);
    ctx.Load(newSurveyList);
    ctx.ExecuteQuery();

    If you want to set "AllowMultipleResponses" to true , we can use Lists.asmx web service to achieve it.

    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 = "First Survey";
    ndDescriptionAttrib.Value = "Custom 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);
    
    
    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));
        }
    }
    

    More information is here:

    https://piyushksingh.com/tag/sharepoint-survey-list-allowmultiresponses/

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Sunday, April 23, 2017 5:30 AM
    Moderator