5

I am working on a site that makes use of a web service to upload documents to Sharepoint. I have a web service that works and uploads to Sharepoint. However, I need to add metadata to this file being uploaded, such as first name, last name, date of birth etc from a database record, as well as live data from the site. This data is stuff like a 'Workflow number', 'Agreement number', 'Document Type' which gets generated on the site and is associated to that Member and document.

Here is the Code:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using Microsoft.SharePoint.Client;

namespace DCTMAgent
{
    public class SharePoint
    {               
        internal void SPUploader(Stream fs, string fn)
        {
            ClientContext context = new ClientContext("http://SharepointSite/Home.aspx");

            System.Net.ICredentials creds = System.Net.CredentialCache.DefaultCredentials;

            context.Credentials = creds;
            context.RequestTimeout = 60000000; // Time in milliseconds

            string url = "/Members/";
            string fileName = Path.GetFileName(fn);                   

            string fnUrl = url + fn;
            Microsoft.SharePoint.Client.File.SaveBinaryDirect(context, fnUrl, fs, true);           
        }       
    }
}

How can I add metadata to this file being uploaded?

1

1 Answer 1

12

Here is my solution:

    public class SharePoint
        {
            internal void SPUploader(Stream fs, string fn)
             {
                ClientContext context = new ClientContext("http://Sharepointsite");///SitePages/Home.aspx");
                System.Net.ICredentials creds = System.Net.CredentialCache.DefaultCredentials;

                context.Credentials = creds;
                context.RequestTimeout = 60000000; // Time in milliseconds

                string url = "/Members/";
                string fileName = Path.GetFileName(fn);                   

                string fnUrl = url + fn;

                Microsoft.SharePoint.Client.File.SaveBinaryDirect(context, fnUrl, fs, true);

                string uniqueRefNo = GetNextDocRefNo();

                SP.Web web = context.Web;

                Microsoft.SharePoint.Client.File newFile = web.GetFileByServerRelativeUrl(fnUrl);
                context.Load(newFile);
                context.ExecuteQuery();

                Web site = context.Web;
                List docList = site.Lists.GetByTitle("Members");

                context.Load(docList);
                context.ExecuteQuery();


                context.Load(docList.Fields.GetByTitle("Workflow Number"));
                context.Load(docList.Fields.GetByTitle("Agreement Number"));
                context.Load(docList.Fields.GetByTitle("First Name"));
                context.Load(docList.Fields.GetByTitle("Surname"));
                context.Load(docList.Fields.GetByTitle("ID Number"));
                context.Load(docList.Fields.GetByTitle("Date of Birth"));
                context.Load(docList.Fields.GetByTitle("Country"));
                context.Load(docList.Fields.GetByTitle("Document Description"));
                context.Load(docList.Fields.GetByTitle("Document Type"));
                context.Load(docList.Fields.GetByTitle("Document Group"));

                context.ExecuteQuery();                                

*********NEED TO GET THE INTERNAL COLUMN NAMES FROM SHAREPOINT************
                var name = docList.Fields.GetByTitle("Workflow Number").InternalName;
                var name2 = docList.Fields.GetByTitle("Agreement Number").InternalName;
                var name3 = docList.Fields.GetByTitle("First Name").InternalName;
                var name4 = docList.Fields.GetByTitle("Surname").InternalName;
                var name5 = docList.Fields.GetByTitle("ID Number").InternalName;
                var name6 = docList.Fields.GetByTitle("Date of Birth").InternalName;
                var name7 = docList.Fields.GetByTitle("Country").InternalName;
                var name8 = docList.Fields.GetByTitle("Document Description").InternalName;
                var name9 = docList.Fields.GetByTitle("Document Type").InternalName;
                var name10 = docList.Fields.GetByTitle("Document Group").InternalName;
                var name11 = docList.Fields.GetByTitle("Unique Document Ref No").InternalName;     

**********NOW SAVE THE METADATA TO SHAREPOINT**********
                newFile.ListItemAllFields[name] = "927015";
                newFile.ListItemAllFields[name2] = "1234565";
                newFile.ListItemAllFields[name3] = "Joe";
                newFile.ListItemAllFields[name4] = "Soap";
                newFile.ListItemAllFields[name5] = "7502015147852";
                newFile.ListItemAllFields[name6] = "1975-02-01";
                newFile.ListItemAllFields[name7] = "ZA";
                newFile.ListItemAllFields[name8] = "Test";
                newFile.ListItemAllFields[name9] = "Requirements";
                newFile.ListItemAllFields[name10] = "Requirements";
                newFile.ListItemAllFields[name11] = uniqueRefNo;

                newFile.ListItemAllFields.Update();
                context.Load(newFile);
                context.ExecuteQuery();
Sign up to request clarification or add additional context in comments.

2 Comments

please can you implement the same using SharePoint server object model
Missing implementation details of GetNextDocRefNo();

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.