Nick Grattan's Blog

About Microsoft SharePoint, .NET, Natural Language Processing and Machine Learning

Trimming WebPart Output with SPSecurityTrimmedControl

with one comment

The SPSecurityTrimmedControl can be used in your SharePoint pages and web parts to conditionally display content based on permissions assigned to a user.

Here’s an example of using the control in a web part. The control is declared in Microsoft.SharePoint (in microsoft.sharepoint.dll). First, create an instance:

SPSecurityTrimmedControl stcEditItem= new SPSecurityTrimmedControl();
stcEditItem.Permissions = SPBasePermissions.EditListItems;

Next, specify the output to display only if the current user has the given permissions. In this case

  • Define a literal server control, add some content
  • Add the literal to the SPSecurityTrimmedControl
  • Add the SPSecurityTrimmedControl to the controls collection for the web part:

Literal litEditItem = new Literal();
litEditItem.Text = “My Content”;

Finally, you can explicitly render the SPSecurityTrimmedControl control in the web part’s “Render” method:


The content “My Content” will only be displayed in the web part if the user has the “EditListItems” permission.

Also, you can programmatically test for permissions on various SharePoint objects, such as SPListItem, using the AllRolesForCurrentUser method:

SPListItem li;

if (!li.AllRolesForCurrentUser.Contains(web.RoleDefinitions[“Approve”]))

This web post from Richard Harbridge shows how to use the SPSecurityTrimmedControl in an ASPX page.

Written by Nick Grattan

November 13, 2008 at 1:05 pm

One Response

Subscribe to comments with RSS.

  1. hey nick, thanks for this info. i have spent the last hour trying to find the best way to do this, never thought to use the allrolesforcurrentuser method. great post!


    December 10, 2008 at 5:30 am

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: