开发者

Adding custom properties to a site which are availble via code

I have been asked to do something and i am very new to Sharepoint development and customisation so am a bit unsure how to go about it, or if it is even possible. The brief is to create a web part that shows a list of sub sites of the current site, i have messed around and have created a web part which does this.

The second part of the problem is this list needs to be filtered but its current active state and also what type of site it is, which means i only want to show Active Projects which are of the type Work Projects. Is it possible to create properties like this for a site in sharepoint, and how can i access them to perform some form of filtering, maybe by just using a foreach loop or using LINQ, i am just a bit confused about how i can go about this and am struggling to find anything re开发者_开发技巧ally concrete on this.

Thanks,

Rob


This is possible. The SPWebApplication and the SPWeb objects both have a so-called "Property Bag". This property bag is accesible through the Properties and the AllProperties respectively in mentioned objects. SPWebApplication is an SPPersistedObject from which it inherits the property bag.

You can store values in this property bag through code (and through SharePoint Designer for the SPWeb object). The property bag should only be accesible by the system account, so any code accessing the property bag should be surrounded by a SPSecurity.RunWithElevatedPrivileges block:

SPSecurity.RunWithElevatedPrivileges(delegate {
  // value can be any object, 
  // but I recommend sticking to primitive types like string, int etc. only
  SPContext.Current.Site.RootWeb.Properties[key] = value;
  SPContext.Current.Site.RootWeb.AllProperties[key] = value;
  SPContext.Current.Site.RootWeb.Update();
  SPContext.Current.Site.RootWeb.Properties.Update();
});

The SPWeb object has both aProperties and an AllProperties property. As you can see, I update both property bag, you can read why in this more in-depth article about the property bag here.

P.S.

I only mention the SPWebApplication also here for completeness, you should stick with the SPWeb. I ususally store values in one place, and that's the current SPSite's RootWeb.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜