Add Meta Properties to SharePoint List

Standard

Sometime you want to associate your list with some other information which is a related to the list it self no to the items
One may though that using hidden field is solve this issue but it is not incredible to add a certain value to every item with the same value and what if there is no items created yet example of that you need to add information about expire date of a survey list
I have found the best solution is to add it to the RootFolder properties which is metadata about the current list
Here is an example
1- To add property at creating list
SPSecurity.RunWithElevatedPrivileges(delegate() {
using (SPSite site = new SPSite(SPContext.Current.Site.ID))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
Guid listID = web.Lists.Add(TextBox1.Text, “”, SPListTemplateType.Survey);
SPList list = web.Lists[listID];
SPFolder folder = list.RootFolder;
folder.Properties.Add(“Test”, web.CurrentUser.LoginName);
folder.Update();
list.Update();
web.Update();
web.AllowUnsafeUpdates = false;
}
}

});

2- To retierve this value
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(SPContext.Current.Site.ID))
{
using (SPWeb web = site.OpenWeb())
{
Label1.Text = web.Lists[TextBox1.Text].RootFolder.Properties[“Test”].ToString();
}
}

});

Hope this post help you all

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s