1

I want to GET documents With SSOM in SP2007 and upload with REST to SP2016

This SSOM code for create doc library: (Working):

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);
}

This is my code for add document in SharePoint 2016: (NOT WORKING)

 private static void AddDocumentToNewPortal(byte[] fileArray, string fileName)
{
    var digest = GetFormDigest();

    var url = string.Format(siteURL + "/_api/web/GetFolderByServerRelativeUrl('AQAPs')/Files/Add(url='{0}', overwrite=true)", fileName);
    var body = fileArray.Length.ToString();

    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[HttpRequestHeader.ContentLength] = fileArray.Length.ToString();
    client.Headers["X-RequestDigest"] = digest;
    var json = client.UploadString(url, body);
}

What can I do to add my document code to work?

  • 1
    Please, double check SP versions - "SharePoint 2017" does not exist. :) – Kristaps Vilerts Nov 21 '18 at 12:41
  • sorry, sharepont 2016.. – Goshky Nov 21 '18 at 12:46
  • Please help me... – Goshky Nov 21 '18 at 12:56
  • Since You are working in server side code (C#), could You use "Server Object Model" ? – Kristaps Vilerts Nov 21 '18 at 13:11
  • CSOM is implemented in SP10, I use Server Object model, because i working with sharepoint 07 – Goshky Nov 21 '18 at 13:14
2

The following code for your reference.

//Method to upload File
public static bool AddDocumentToNewPortal(byte[] fileArray, string siteurl, string fileName)
{
    bool status = false;           
    string result = string.Empty;
    //Url to upload file
    string resourceUrl = siteurl + "/_api/web/GetFolderByServerRelativeUrl('AQAPs')/Files/add(url='" + fileName + "',overwrite=true)";
    HttpWebRequest wreq = HttpWebRequest.Create(resourceUrl) as HttpWebRequest;
    wreq.UseDefaultCredentials = false;
    //credential who has edit access on document library
    NetworkCredential credentials = new System.Net.NetworkCredential("username", "password", "domain");
    wreq.Credentials = credentials;

    //Get formdigest value from site
    string formDigest = GetFormDigestValue(siteurl, credentials);
    wreq.Headers.Add("X-RequestDigest", formDigest);
    wreq.Method = "POST";
    wreq.Timeout = 1000000; //timeout should be large in order to upload file which are of large size
    wreq.Accept = "application/json; odata=verbose";
    wreq.ContentLength = fileArray.Length;
    try
    {
        using (System.IO.Stream requestStream = wreq.GetRequestStream())
        {
            requestStream.Write(fileArray, 0, fileArray.Length);
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
    try
    {
        WebResponse wresp = wreq.GetResponse();
        using (System.IO.StreamReader sr = new System.IO.StreamReader(wresp.GetResponseStream()))
        {
            result = sr.ReadToEnd();
            status = true;
            return status;
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
        return status;
        throw;
    }
}

//Method which return form digest value
private static string GetFormDigestValue(string siteurl, NetworkCredential credentials)
{
    string newFormDigest = "";
    HttpWebRequest endpointRequest = (HttpWebRequest)HttpWebRequest.Create(siteurl + "/_api/contextinfo");
    endpointRequest.Method = "POST";
    endpointRequest.ContentLength = 0;
    endpointRequest.Credentials = credentials;
    endpointRequest.Accept = "application/json;odata=verbose";
    try
    {
        HttpWebResponse endpointResponse = (HttpWebResponse)endpointRequest.GetResponse();
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
    try
    {
        WebResponse webResp = endpointRequest.GetResponse();
        Stream webStream = webResp.GetResponseStream();
        StreamReader responseReader = new StreamReader(webStream);
        string response = responseReader.ReadToEnd();
        var j = JObject.Parse(response);
        var jObj = (JObject)JsonConvert.DeserializeObject(response);
        foreach (var item in jObj["d"].Children())
        {
            newFormDigest = item.First()["FormDigestValue"].ToString();
        }
        responseReader.Close();
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
    return newFormDigest;
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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