4

I am working on a requirement where I need to create Document Library using REST API using C# code using Visual Studio.

The main problem is that I am not having SharePoint Server environment. I am just having site URL and credentials.

When I use following code to get the data from list item then that works properly. But now I want to create document library. So can any one suggest what should I change in my code so that it can create a document library. I tried various codes with POST but that are of no use for posting here.

My GET code is as follow:

SecureString passWord = new SecureString();
            foreach (char c in "Password".ToCharArray())
                passWord.AppendChar(c);
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("SiteURL/_api/web/lists/getbytitle('ListName')/items?$select=id,LastName");
        request.Method = "GET";
        request.Accept = "application/json;odata=verbose";
        request.ContentType = "application/json;odata=verbose";
        request.Credentials = new NetworkCredential("username", passWord, "siteurl");
        WebResponse response = request.GetResponse();

        string base64Credentials = GetEncodedCredentials();

        //Read the returned posts into an object that can be consumed by the calling application
        using (response)
        {
            using (var reader = new StreamReader(response.GetResponseStream()))
            {
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                try
                {
                    string jSON = reader.ReadToEnd();
                }
                catch (Exception ex)
                {
                    throw new Exception(string.Format("An error occurred when reading the list items from SharePoint: {0}; {1}", ex.Message, ex.StackTrace));
                }
            }
        }

2 Answers 2

2

Doing this using the Client Object Model would be much easier. Unless you have a reason that you absolutely need to use the REST API, I suggest you switch.

That said, here's the code you need to create a list using the REST API:

private ICredentials creds = new NetworkCredential("username", "password", "domain");

private string GetFormDigest()
{
    var url = siteUrl + "/_api/contextinfo";
    var client = new WebClient();
    client.Credentials = creds;
    client.Headers[HttpRequestHeader.Accept] =
        "application/json;odata=verbose";
    var json = client.UploadString(url, "");

    var ser = new JavaScriptSerializer();
    dynamic item = ser.Deserialize<object>(json);

    var digest = item["d"]["GetContextWebInformation"]["FormDigestValue"];

    return digest;
}

private void CreateList()
{
    var digest = GetFormDigest();

    var url = siteUrl + "/_api/Web/Lists";
    var body = "{'__metadata': { type: 'SP.List' }, " +
        "BaseTemplate: 107, " +
        "Title: 'Tasks'}";

    var client = new WebClient();
    client.Credentials = creds;
    client.Headers[HttpRequestHeader.Accept] =
        "application/json;odata=verbose";
    client.Headers[HttpRequestHeader.ContentType] =
        "application/json;odata=verbose";
    client.Headers["X-RequestDigest"] = digest;
    var json = client.UploadString(url, body);
}
0
2

Sample code to create document library .

url: http://site url/_api/web/lists

method: POST

body: { '__metadata': { 'type': 'SP.List' }, 'AllowContentTypes': true, 'BaseTemplate': 101,

 'ContentTypesEnabled': true, 'Description': 'My list description', 'Title': 'Test' }

Headers: 
    Authorization: "Bearer " + accessToken
    X-RequestDigest: form digest value
    accept: "application/json;odata=verbose"
    content-type: "application/json;odata=verbose"
    content-length:length of post body 

Hope this will help you

1
  • Thanks but I have this, I need from c# calling REST API using C#. Commented Jul 4, 2016 at 11:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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