Hide Webpart for Anonymous users, show only to logged in users

By | 2015-11-17

When searching for a simple and elegant solution to hide a webpart for anonymous users and show it only to logged in users, I couldn’t find a Powershell way to do it.

Though it was quite easy in retrospect when I found the CSharp code and explored the possibilities of the out of the box Audience.

AudienceManager audienceManager = new AudienceManager(ServerContext.Current);
webPartManager.WebParts[0].AuthorizationFilter = string.Format(“{0};;;;”, audienceManager.GetAudience(“Audience 1”).AudienceID);

The powershell way:

 

Of course you need a Web, a Page and a WebPartManager and  then create a new webpart or get an existing webpart.

A working example where a new webpart is added on the welcomepage:

 

Leave a Reply

Your email address will not be published. Required fields are marked *