Archive for March 21, 2014

Set Title Field on Document Upload

When uploading a file to a SharePoint library through code, you sometimes want to set item properties at the same time the document is created. To achieve this you simply populate a Hashtable object and pass this into the Add method of the SPFileCollection object:

public SPFile UploadDocument(SPDocumentLibrary library, string filename, string docTitle, byte[] fileBinary)
{
    SPFileCollection files = library.RootFolder.Files;
    System.Collections.Hashtable hash = new System.Collections.Hashtable();
    hash.Add("Title", docTitle);
 
    SPFile file = files.Add(filename, fileBinary, hash, false);
    return file;
}

There is an issue with the above method when trying to set the value for the Title field. After adding the file, you’ll find the Title column is left blank and SharePoint has not set its value. To have the value set, you have a couple of options.
Read more

Follow

Get every new post delivered to your Inbox

Join other followers: