如何仅在特定用户或案例中隐藏sharepart中的webpart

时间:2012-09-26 11:20:12

标签: sharepoint sharepoint-2010 web-parts sharepoint-api

我有PublishingLayoutPage并且依赖于查询参数我应该隐藏几个webparts。

但我现在不知道如何做到这一点。因为如果我正确理解问题 SPLimitedWebPartManager将为所有用户隐藏webpart!

            using (SPLimitedWebPartManager mgr = file.GetLimitedWebPartManager(PersonalizationScope.Shared))
                        {
                            try
                            {
                                SPLimitedWebPartCollection webparts = mgr.WebParts;
                                foreach (System.Web.UI.WebControls.WebParts.WebPart wp in webparts)
                                {
                                    if (wp is MyWebpart && !isShowWebpart)
                                    {
wp.Hidden=true;
                                        mgr.SaveChanges(wp );
                                    }
                                }

如何隐藏webpart并且不允许仅为页面上的特定案例呈现内容?

1 个答案:

答案 0 :(得分:1)

听起来你可以把它放在你的Load / PreRender /适当的地方:

    List<string> webPartTitlesToHide = new List<string>();
    webPartTitlesToHide.Add("Long Title");
    webPartTitlesToHide.Add("Long Content");
    webPartTitlesToHide.Add("Long Footer");


    if (Request["view"] == "short")
    {
        WebPartManager wpm = WebPartManager.GetCurrentWebPartManager(this);
        foreach (WebPart wp in wpm.WebParts)
        {
            if (webPartTitlesToHide.Contains(wp.Title))
            { 
                wpm.CloseWebPart(wp);
            }
        }
    }

显然,您可能有更好的方法来选择要隐藏的Web部件,但这只是一个示例。