{"id":1249,"date":"2012-09-12T10:51:29","date_gmt":"2012-09-12T10:51:29","guid":{"rendered":"https:\/\/poiseddevelopers.com\/reality-tech\/?p=1249"},"modified":"2024-05-13T10:55:33","modified_gmt":"2024-05-13T10:55:33","slug":"folder-metadata-web-part","status":"publish","type":"post","link":"https:\/\/poiseddevelopers.com\/reality-tech\/folder-metadata-web-part\/","title":{"rendered":"Folder Metadata Web Part"},"content":{"rendered":"<h2>Creating a Folder Metadata Web Part<\/h2>\n<p>An interesting challenge I faced was programmatically exposing custom folder metadata within a specific Document Library View, for specific Folder Content Types.<\/p>\n<p>As background, I already created Folder Content Types, deriving from the SharePoint Folder Content Type, to which I added appropriate metadata.<\/p>\n<p>These new Content Types were syndicated from a Content Type Hub, and propagated across the enterprise.<\/p>\n<p>I extended the ASP template to include a table that I will build dynamically and a button that I optionally hide:<\/p>\n<pre lang=\"php\"> \r\n&lt;\/asp:Table&lt;&gt;asp:Button ID=\"PlaceholderButton\" runat=\"server\" Text=\"Create New Placeholder\" onclick=\"PlaceholderButton_Click\" \/&gt;<\/pre>\n<p>I then add an action for the button:<\/p>\n<pre lang=\"php\"> protected void PlaceholderButton_Click(object sender, EventArgs e) \r\n        {\r\n            Response.Redirect(\"http :\/\/SharePoint\/div\/myDiv\/Lists\/MyListTasks\/NewForm.aspx?RootFolder=\"); \r\n        }<\/pre>\n<p>This useful function adds a table row consisting pair of table cells with a property label (in bold) and the property value, but only if the property\/value is found, on the current Document. Note Content Type is detected and handled separately:<\/p>\n<pre lang=\"php\">void push_property(string myLabel, string propertyName)\r\n {\r\n     try\r\n     {\r\n         string val = null;\r\n         if (propertyName == \"ContentType\")\r\n             val = folder.Item.ContentType.Name;\r\n         else\r\n             val = folder.Item.Properties[propertyName].ToString();\r\n \r\n         if (!String.IsNullOrEmpty(val))\r\n         {\r\n             TableRow rw = new TableRow();\r\n             TableCell cel = new TableCell();\r\n             cel.Text = string.Concat(\"<b>\", myLabel, \"<\/b>\");\r\n              \r\n             rw.Cells.Add(cel);\r\n             cel = new TableCell();\r\n             cel.Text = val;\r\n             rw.Cells.Add(cel);\r\n             Table1.Rows.Add(rw);\r\n         }\r\n \r\n     }\r\n     catch { }\r\n \r\n }<\/pre>\n<p>This is the heart of this little web-part. I derive the actual folder from the user context, by extracting it from the browser URL string using Page.Request.QueryString[\u201cRootFolder\u201d]:<\/p>\n<pre lang=\"php\"> protected void Page_Load(object sender, EventArgs e)\r\n{\r\n    bool ok = true;\r\n    string ctName = null;\r\n                 \r\n    SPWeb web = SPContext.Current.Web;\r\n    string rootFolder = Page.Request.QueryString[\"RootFolder\"];\r\n \r\n    \/\/Label2.Text = rootFolder;\r\n \r\n    if (String.IsNullOrEmpty(rootFolder))\r\n        ok = false;\r\n     \r\n \r\n    if (ok)\r\n    {\r\n        folder = web.GetFolder(rootFolder);\r\n        if (!folder.Exists)\r\n            ok=false;\r\n    }\r\n \r\n    if (ok)\r\n    {\r\n        ctName = folder.Item.ContentType.Name;\r\n \r\n        if ((ctName != \"Divison Acct Folder\") &amp;&amp; (ctName != \"Divison Common Folder\"))\r\n          ok=false;\r\n    }\r\n     \r\n        PlaceholderButton.Visible = ok;  \/\/reacts dynamically, needs setting in both directions, as it maintains state\r\n \r\n    if (ok)\r\n    {\r\n        \/\/push_property(\"Folder Type\", \"ContentType\");  \/\/Handled in special fashion internal to function\r\n        push_property(\"Claimant\", \"Claimant\");      \r\n        push_property(\"Account Number\", \"AccountNumber\");\r\n        push_property(\"Issue Description\", \"IssueDescription\");\r\n \r\n        \/*Only apply this border logic if you want a griddy view\r\n        if (Table1.Rows.Count &gt;  0)\r\n        {\r\n            Table1.GridLines = (GridLines) 3;\r\n        }\r\n        *\/\r\n    }\r\n   \r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Creating a Folder Metadata Web Part An interesting challenge I faced was programmatically exposing custom folder metadata within a specific Document Library View, for specific Folder Content Types. As background, I already created Folder Content Types, deriving from the SharePoint Folder Content Type, to which I added appropriate metadata. These new Content Types were syndicated [&hellip;]<\/p>\n","protected":false},"author":12,"featured_media":1251,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[28],"tags":[],"class_list":["post-1249","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"acf":[],"_links":{"self":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/1249","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/comments?post=1249"}],"version-history":[{"count":2,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/1249\/revisions"}],"predecessor-version":[{"id":3971,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/1249\/revisions\/3971"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/media\/1251"}],"wp:attachment":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/media?parent=1249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/categories?post=1249"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/tags?post=1249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}