{"id":6191,"date":"2017-08-09T18:28:09","date_gmt":"2017-08-09T12:58:09","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=6191"},"modified":"2022-09-02T15:48:39","modified_gmt":"2022-09-02T10:18:39","slug":"set-the-metadata-of-a-sharepoint-file-using-rest","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2017\/08\/set-the-metadata-of-a-sharepoint-file-using-rest\/","title":{"rendered":"Set the metadata of a SharePoint file using REST"},"content":{"rendered":"<p><strong>Introduction:<\/strong><\/p>\n<p>In our earlier <a href=\"https:\/\/www.inogic.com\/blog\/2015\/10\/programmatically-create-folders-and-upload-files-in-sharepoint-server-2013-through-plug-inworkflow\/\" target=\"_blank\" rel=\"noopener noreferrer\">blog<\/a>, we discussed about creating folders and uploading files on the SharePoint Online through workflows\/plugins using REST.<\/p>\n<p>In this blog, we will see how we can set the metadata of the uploaded files using REST.<!--more--><\/p>\n<p><strong>Below is the code snippet to set the<\/strong><strong> metadata of a file using REST:<\/strong><\/p>\n<pre class=\"lang:default decode:true\">\/\/Here first create the JSON string of file metadata\r\nstring result = @\"{'__metadata': { 'type': 'SP.ListItem'}, 'Aliases':'My File.docx', 'Owner':'CRM Owner', 'Date':'1\/7\/2017'}\";\r\nstring siteUrl = \u201chttps:\/\/crmtrial.sharepoint.com\u201d \r\nUri spSite = new Uri(siteUrl);\r\nprivate SpoAuthUtility _spo = SpoAuthUtility.Create(spSite, _username, WebUtility.HtmlEncode(_password), false);\r\n\/\/read the digest\r\nstring digest = _spo.GetRequestDigest();\r\n\/\/convert JSON data to bytes\r\nbyte[] content = ASCIIEncoding.ASCII.GetBytes(result);\r\n\r\n\/\/define the file URL to set the metadata\r\nstring fileURL = Account\/Test Account Folder\/TestFile.docx;\r\nvar requestUrl = string.Format(\"{0}\/_api\/web\/GetFileByServerRelativeUrl('{1}')\/ListItemAllFields\", siteURL, fileURL);\r\n\r\nvar webRequest = (HttpWebRequest)HttpWebRequest.Create(requestUrl);\r\n                webRequest.Headers.Add(\"X-RequestDigest\", digest);\r\n                webRequest.Headers.Add(\"X-HTTP-Method\", \"PATCH\");\r\n                webRequest.Headers.Add(\"If-Match\", \"*\");\r\n                webRequest.Accept = \"application\/json;odata=verbose\";\r\n                webRequest.ContentType = \"application\/json;odata=verbose\";\r\n                webRequest.Method = \"POST\";\r\n\r\n                \/\/ Send a json odata request to SPO rest services to fetch all list items for the list.\r\n                byte[] result = HttpHelper.SendODataJsonRequest(\r\n                  new Uri(requestUrl),\r\n                  \"POST\", \/\/ reading data from SP through the rest api usually uses the GET verb \r\n                  content,\r\n                  webRequest,\r\n                  _spo \/\/ pass in the helper object that allows us to make authenticated calls to SPO rest services\r\n                  );\r\n\r\n                \/\/read the response\r\n                string response = Encoding.UTF8.GetString(result, 0, result.Length);<\/pre>\n<p>If you go to the uploaded file\u2019s properties in SharePoint, you can see the metadata of the file as shown in the screenshot below;<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-6192\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2017\/08\/Set-the-metadata-of-a-SharePoint-file-using-REST.png\" alt=\"Set the metadata of a SharePoint file using REST\" width=\"604\" height=\"289\" \/><\/p>\n<p>Hope this helps!<\/p>\n<h2 style=\"text-align: left;\"><div class=\"su-heading su-heading-style-default su-heading-align-center\" id=\"\" style=\"font-size:15px;margin-bottom:5px\"><div class=\"su-heading-inner\">Generate Your Own New Leads Within Microsoft Dynamics 365 CRM<\/div><\/div><\/h2>\n<p><em>Contact us for a <a href=\"https:\/\/www.maplytics.com\/maplytics-download\/?utm_source=highvisits&amp;utm_medium=technicalblog&amp;utm_campaign=hMaplytics\" target=\"_blank\" rel=\"noopener\">demo<\/a> to know more about how <a href=\"https:\/\/www.maplytics.com\/?utm_source=highvisits&amp;utm_medium=technicalblog&amp;utm_campaign=hMaplytics\" target=\"_blank\" rel=\"noopener\">Maplytics<\/a> can help you to generate new leads from within Microsoft Dynamics 365 CRM.<\/em><\/p>\n<p><em><a href=\"https:\/\/www.maplytics.com\/?utm_source=highvisits&amp;utm_medium=technicalblog&amp;utm_campaign=hMaplytics\" target=\"_blank\" rel=\"noopener\">Maplytics<\/a> is a 5-star rated, preferred business app on the <a href=\"https:\/\/appsource.microsoft.com\/en-us\/product\/dynamics-365\/inogic.f6f3c73f-29de-4fa8-a396-87ea8a07b6c4?tab=Overview\" target=\"_blank\" rel=\"noopener\">Microsoft AppSource<\/a> that is Certified for Microsoft Dynamics 365 (CfMD) and comes with powerful features like Appointment Planning, Sales Routing, Territory Management, Heat Maps, Geo-analytical Dashboards and more that empower organizations to add more value to their CRM data, improve sales &amp; service processes, and achieve high ROI.<\/em><\/p>\n<p><em>Get your <a href=\"https:\/\/www.maplytics.com\/maplytics-download\/?utm_source=highvisits&amp;utm_medium=technicalblog&amp;utm_campaign=hMaplytics\" target=\"_blank\" rel=\"noopener\">free trial<\/a> from our Website or <a href=\"https:\/\/appsource.microsoft.com\/en-us\/product\/dynamics-365\/inogic.f6f3c73f-29de-4fa8-a396-87ea8a07b6c4?tab=Overview\" target=\"_blank\" rel=\"noopener\">Microsoft AppSource<\/a>!<\/em><\/p>\n<p><em>&#8216;If data is the new oil, location intelligence is ??\u201d<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction: In our earlier blog, we discussed about creating folders and uploading files on the SharePoint Online through workflows\/plugins using REST. In this blog, we will see how we can set the metadata of the uploaded files using REST.<\/p>\n","protected":false},"author":13,"featured_media":6194,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[16,19,53],"tags":[1460,1596,1609],"class_list":["post-6191","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dynamics-365","category-dynamics-crm","category-sharepoint","tag-rest","tag-set-the-metadata-of-a-sharepoint-file","tag-sharepoint"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/6191","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/comments?post=6191"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/6191\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media\/6194"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=6191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=6191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=6191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}