如何将Web部件定位到受众

时间:2011-08-12 06:52:13

标签: c# sharepoint-2010

您好我是sharepoint的新手。如何以编程方式将Web部件定位到受众建议任何答案。

1 个答案:

答案 0 :(得分:1)

根据link

要为特定SharePoint组的sharepoint Web部件提供权限,我们必须使用AudienceManager,如下所示

SPWeb currentSite = SPContext.Current.Site.RootWeb;
SPSite spSite = currentSite.Site;

SPServiceContext sc = SPServiceContext.GetContext(spSite);
AudienceManager am = new AudienceManager(sc);

Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager mgr = null;
mgr = currentSite.GetLimitedWebPartManager("SitePages" + "/" + page,         
      System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
foreach (System.Web.UI.WebControls.WebParts.WebPart webPart in mgr.WebParts)
{
     if (webPart.Title == "WebPartTitle")
     {
          //Get the Existing Audience for the Web Part
          string audWebPart = webPart.AuthorizationFilter;
          //If there is no Audience for the Web Part just add it
          if (audWebPart == "")
          {
               webPart.AuthorizationFilter = ";;;;" + NewAudience;
          }
          //If already Audiences are exist for the Web Part then append new Audience
          else
          {
               webPart.AuthorizationFilter = audWebPart + "," + NewAudience;
          }
          mgr.SaveChanges(webPart);
          lblMessage.Text = "Succeeded...";
     }
}

如果我们只设置AuthorizationFilter属性的值,那么现有的受众群体将被覆盖。 首先要避免它,我们必须检查该Web部件是否已存在受众。如果是这样,我们必须追加它。

SharePoint Server 2010框架要求三种值由一对分号(“;;”)分隔。全局受众和SharePoint组的多个值由逗号分隔,并且分发列表的多个值由换行符(“\ n”)分隔。因此在上面的例子中我们用“;;;;”为AuthorizationFilter属性提供有效的字符串。