使用代码为SharePoint页面上的WebParts设置访问群体

WebParts的访问群体,日文系统中叫[対象ユーザー]

 

通过使用目标访问群体,可以将内容(如列表或库项目、导航链接以及整个 Web 部件 (Web 部件:由标题栏、框架和内容组成的信息模块单元。Web 部件是 Web 部件页的基本结构块。))显示给特定用户组。在希望仅提供与特定用户组相关的信息时,此功能非常有用。例如,可以将 Web 部件添加到法律部门的门户网站,其中包含仅对该部门可见的有效合同列表。

 

要确定目标访问群体,可以使用下列一项或多项:
・SharePoint 组
・通讯组列表
・安全组
・全局访问群体 
     (全局访问群体是基于规则的访问群体,由 SharePoint 管理员维护。)
只要访问群体的名称已知,至少具有参与者权限 (权限:对执行特定操作(例如查看页面、打开项目和创建子网站)的授权。)的任何人都可以指定目标访问群体。
 

代码实现

using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.SharePoint;

using Microsoft.SharePoint.Administration;

using Microsoft.SharePoint.WebPartPages;

using System.Web;

using System.Web.UI.WebControls.WebParts;

using Microsoft.Office.Server;

using Microsoft.Office.Server.Audience;

 

namespace ConsoleApplication2

{

    class Program

    {

        static void Main(string[] args)

        {

            try

            {

                using (SPSite mySPSite = new SPSite("<SPSiteURL>"))

                {

                    SPWeb spRootWeb = mySPSite.RootWeb;

 

                    SPLimitedWebPartManager wpManager = spRootWeb.GetLimitedWebPartManager(spRootWeb.Url + "/default.aspx", PersonalizationScope.Shared);

                    SPLimitedWebPartCollection wpCol = wpManager.WebParts;

                    //历页面上的WebParts找目WebParts

                    foreach (System.Web.UI.WebControls.WebParts.WebPart webp in wpCol)

                    {

                        //根据Title取得目WebParts,也可以选择其他唯一性的标识

                        if (webp.Title == "<TheTitleOFYourWebParts>")

                        {

                            string audienceId = string.Empty;

                            string audienceName = "<全局访问群体名>";

 

                            ServerContext context = ServerContext.GetContext(mySPSite);

                            AudienceManager audManager = new AudienceManager(context);

                            AudienceCollection audCol = audManager.Audiences;

                            //取得全局访问群体的GUID

                            foreach (Audience audience in audCol)

                            {

                                if (audience.AudienceName.Equals(audienceName))

                                {

                                    audienceId = audience.AudienceID.ToString();

                                    break;

                                }

                            }

                            //检查全局访问群体是否存在,否抛出错误

                            if (string.IsNullOrEmpty(audienceId))

                            {

                                throw new ApplicationException(

                                    "<错误信息>",

                                    null);

                            }

                            //全局访问全体以GUID的形式存在,多个的情况下用逗号分

                            string[] globalAudienceIDs = new string[] { audienceId };

                            //安全以如下形式存

                            string[] dlDistinguishedNames = new string[] { "cn=group1,cn=users,dc=spdev,dc=com", "cn=group2,cn=users,dc=spdev,dc=com" };

                            //SharePointGroupGroupName的形式存在

                            string[] sharePointGroupNames = new string[] { "<SharePointGroupName1>", "<SharePointGroupName2>" };

                            //使用MOSS提供的方法格式化即将添加的访问群体,需要using Microsoft.Office.Server.Audience

                            string webPartAudiences = AudienceManager.GetAudienceIDsAsText(

                                globalAudienceIDs,

                                dlDistinguishedNames,

                                sharePointGroupNames);

                            webp.AuthorizationFilter = webPartAudiences;

                            //善后理,也某个是多余的...

                            webp.DataBind();

                            wpManager.SaveChanges(webp);

                            break;

                        }

 

                    }

                    wpManager.Dispose();

                    spRootWeb.Update();

                }

            }

            catch (ApplicationException myException)

            {

                <全局访问群体不存在错误处>

            }

            catch (Exception e)

            {

                <错误处>

            }

        }

    }

}

 

 

参考 http://stsadm.blogspot.com/2008/04/programmatically-setting-web-part.htmlsetting-web-part.html 

 

posted on 2009-04-02 16:03  阿米巴原虫  阅读(597)  评论(0编辑  收藏  举报