Search code examples
c#asp.netsharepoint-2010sharepoint-2013

Uploading a document from Windows Store App to a SharePoint 2013 document library


I need to upload a document to a SharePoint 2013 document library (can be via Rest Api/other) using C# & also retrieve the unique id/location of the document currently uploaded. Please suggest to me a recommended way. Any help would be appreciated.


Solution

  • Here is an example of uploading a file and using the server response to get the ID of the document which can be used to get the location though the response may also contain the URL of the document if you poke through the JSON.

    FileOpenPicker picker = new FileOpenPicker();
    picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
    picker.ViewMode = PickerViewMode.Thumbnail;
    // Filter to include a sample subset of file types.
    picker.FileTypeFilter.Clear();
    picker.FileTypeFilter.Add(".bmp");
    picker.FileTypeFilter.Add(".png");
    picker.FileTypeFilter.Add(".jpeg");
    picker.FileTypeFilter.Add(".jpg");
    // Open the file picker.
    StorageFile path = await picker.PickSingleFileAsync();
    if (path != null)
    {
         string url = "https://YourSite.com/Subsite/";
         HttpClient client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true });
         client.BaseAddress = new System.Uri(url);
         client.DefaultRequestHeaders.Clear();
         client.DefaultRequestHeaders.Add("Accept", "application/json;odata=verbose");
         client.DefaultRequestHeaders.Add("X-RequestDigest", digest);
         client.DefaultRequestHeaders.Add("X-HTTP-Method", "POST");
         client.DefaultRequestHeaders.Add("binaryStringRequestBody", "true");
         IRandomAccessStream fileStream = await path.OpenAsync(FileAccessMode.Read);
         var reader = new DataReader(fileStream.GetInputStreamAt(0));
         await reader.LoadAsync((uint)fileStream.Size);
         Byte[] content = new byte[fileStream.Size];
         reader.ReadBytes(content);
         ByteArrayContent file = new ByteArrayContent(content);
         HttpResponseMessage response = await client.PostAsync("_api/web/lists/getByTitle(@TargetLibrary)/RootFolder/Files/add(url=@TargetFileName,overwrite='true')?@TargetLibrary='Project Photos'&@TargetFileName='TestUpload.jpg'", file);
         response.EnsureSuccessStatusCode();
         if (response.IsSuccessStatusCode)
         { 
            //Get file ID from SharePoint
            var info = response.Content.ReadAsStringAsync();
            JsonObject d = JsonValue.Parse(info.Result).GetObject();
            string id = d["d"].GetObject()["ListItemAllFields"].GetObject().GetNamedValue("ID").Stringify();
         }
     }
    

    'digest' is a string you have to retrieve from SharePoint to authenticate any further REST calls. You can find an example of how to do that and any other common task for a Store App for SharePoint here:

    https://arcandotnet.wordpress.com/2015/04/01/sharepoint-2013-rest-services-using-c-and-the-httpclient-for-windows-store-apps/