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 from a Content Type Hub, and propagated across the enterprise.
I extended the ASP template to include a table that I will build dynamically and a button that I optionally hide:
</asp:Table<>asp:Button ID="PlaceholderButton" runat="server" Text="Create New Placeholder" onclick="PlaceholderButton_Click" /> |
I then add an action for the button:
protected void PlaceholderButton_Click(object sender, EventArgs e) { Response.Redirect("http ://SharePoint/div/myDiv/Lists/MyListTasks/NewForm.aspx?RootFolder="); } |
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:
void push_property(string myLabel, string propertyName) { try { string val = null; if (propertyName == "ContentType") val = folder.Item.ContentType.Name; else val = folder.Item.Properties[propertyName].ToString(); if (!String.IsNullOrEmpty(val)) { TableRow rw = new TableRow(); TableCell cel = new TableCell(); cel.Text = string.Concat("<b>", myLabel, "</b>"); rw.Cells.Add(cel); cel = new TableCell(); cel.Text = val; rw.Cells.Add(cel); Table1.Rows.Add(rw); } } catch { } } |
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[“RootFolder”]:
protected void Page_Load(object sender, EventArgs e) { bool ok = true; string ctName = null; SPWeb web = SPContext.Current.Web; string rootFolder = Page.Request.QueryString["RootFolder"]; //Label2.Text = rootFolder; if (String.IsNullOrEmpty(rootFolder)) ok = false; if (ok) { folder = web.GetFolder(rootFolder); if (!folder.Exists) ok=false; } if (ok) { ctName = folder.Item.ContentType.Name; if ((ctName != "Divison Acct Folder") && (ctName != "Divison Common Folder")) ok=false; } PlaceholderButton.Visible = ok; //reacts dynamically, needs setting in both directions, as it maintains state if (ok) { //push_property("Folder Type", "ContentType"); //Handled in special fashion internal to function push_property("Claimant", "Claimant"); push_property("Account Number", "AccountNumber"); push_property("Issue Description", "IssueDescription"); /*Only apply this border logic if you want a griddy view if (Table1.Rows.Count > 0) { Table1.GridLines = (GridLines) 3; } */ } } |
Want to talk?
Drop us a line. We are here to answer your questions 24*7.