web properity

string employeeProfile = GetWebProperty("EmployeeProfileUserGroup");

SetWebProperty("EmployeeProfileUserGroup", pe.DisplayText);

static string GetWebProperty(string Name)
{
 using (SPSite site = new SPSite(SPContext.Current.Site.ID))
 {
  SPWeb web = site.OpenWeb(SPContext.Current.Web.ID);
  if (web.Properties[Name] == null)
  {
   web.AllowUnsafeUpdates = true;
   web.Properties.Add(Name, "");
   web.Properties.Update();
   web.AllowUnsafeUpdates = false;
   return "";
  }
  else
  {
   return web.Properties[Name].ToString();
  }
 }
}
void SetWebProperty(string Name, string Value)
{
 using (SPSite site = new SPSite(SPContext.Current.Site.ID))
 {
  SPWeb web = site.OpenWeb(SPContext.Current.Web.ID);
  GetWebProperty(Name); //ensure the property exists
  web.AllowUnsafeUpdates = true;
  web.Properties[Name] = Value;
  web.Properties.Update();
  web.AllowUnsafeUpdates = false;
 }
}

If you would like to share the same properties across multiple webs, use the Site.RootWeb object and store all properties there!

你可能感兴趣的:(Web)