How to hide webpart in sharepoint only for specific user or case
-
03-07-2021 - |
Question
I have PublishingLayoutPage and depends on query params i should hide few webparts.
But i dont now how to do this. Because if i understand problem correctly SPLimitedWebPartManager will hide webpart for all users!
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 );
}
}
How to hide webpart and doesnot allow to render content only for specific case on page?
Solution
It sounds like you could place this in your Load/PreRender/wherever is appropriate:
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);
}
}
}
Obviously, you probably have a better way of selecting which webparts to hide, but this is just an example.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow