{"id":1653,"date":"2014-03-21T17:17:52","date_gmt":"2014-03-21T17:17:52","guid":{"rendered":"http:\/\/www.stuartroberts.net\/?p=1653"},"modified":"2014-10-18T14:00:15","modified_gmt":"2014-10-18T13:00:15","slug":"set-title-field-document-upload","status":"publish","type":"post","link":"https:\/\/www.stuartroberts.net\/index.php\/2014\/03\/21\/set-title-field-document-upload\/","title":{"rendered":"Set Title Field on Document Upload"},"content":{"rendered":"<p>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 <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.spfilecollection.add.aspx\" title=\"SPFileCollection.Add method\" target=\"_blank\">Add<\/a> method of the <a href=\"http:\/\/msdn.microsoft.com\/library\/Microsoft.SharePoint.SPFileCollection\" title=\"SPFileCollection class\" target=\"_blank\">SPFileCollection<\/a> object:<\/p>\n<pre lang=\"csharp\">\r\npublic SPFile UploadDocument(SPDocumentLibrary library, string filename, string docTitle, byte[] fileBinary)\r\n{\r\n    SPFileCollection files = library.RootFolder.Files;\r\n    System.Collections.Hashtable hash = new System.Collections.Hashtable();\r\n    hash.Add(\"Title\", docTitle);\r\n\r\n    SPFile file = files.Add(filename, fileBinary, hash, false);\r\n    return file;\r\n}\r\n<\/pre>\n<p>There is an issue with the above method when trying to set the value for the Title field.  After adding the file, you&#8217;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.<br \/>\n<!--more--><\/p>\n<ul>\n<li>1. Change the name of the key in the Hashtable from &#8220;Title&#8221; to &#8220;vti_title&#8221;<\/li>\n<li>2. Use the unique identifier of the title field<\/li>\n<\/ul>\n<p>For example:<\/p>\n<pre lang=\"csharp\">\r\npublic SPFile UploadDocument(SPDocumentLibrary library, string filename, string docTitle, byte[] fileBinary)\r\n{\r\n    SPFileCollection files = library.RootFolder.Files;\r\n    System.Collections.Hashtable hash = new System.Collections.Hashtable();\r\n    hash.Add(\"vti_title\", docTitle);\r\n\r\n    SPFile file = files.Add(filename, fileBinary, hash, false);\r\n    return file;\r\n}\r\n<\/pre>\n<p>or, use the built-in <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.spbuiltinfieldid.title(v=office.15).aspx\" title=\"SPBuiltInFieldId.Title field\" target=\"_blank\">SPBuiltInFieldId.Title<\/a> property value:<\/p>\n<pre lang=\"csharp\">\r\npublic SPFile UploadDocument(SPDocumentLibrary library, string filename, string docTitle, byte[] fileBinary)\r\n{\r\n    SPFileCollection files = library.RootFolder.Files;\r\n    System.Collections.Hashtable hash = new System.Collections.Hashtable();\r\n    hash.Add(SPBuiltInFieldId.Title.ToString(), docTitle);\r\n\r\n    SPFile file = files.Add(filename, fileBinary, hash, false);\r\n    return file;\r\n}\r\n<\/pre>\n<p>Using either of the above will result in the title field being set on creation of the document.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/www.stuartroberts.net\/index.php\/2014\/03\/21\/set-title-field-document-upload\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":[]},"categories":[3],"tags":[81],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/plx2I-qF","_links":{"self":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1653"}],"collection":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/comments?post=1653"}],"version-history":[{"count":6,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1653\/revisions"}],"predecessor-version":[{"id":1727,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1653\/revisions\/1727"}],"wp:attachment":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/media?parent=1653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/categories?post=1653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/tags?post=1653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}