Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I realize that content editor web parts can utilize audience targeting in the full version of SharePoint. I'd like to go one step further and actually target content within my CEWP to specific groups.

Example: I have a "navigation" CEWP that displays icons and links. I realize that this same functionality can be recreated using the quick launch, however users do not respond as well to the quick launch as they do to big shiny buttons.

enter image description here

I would like to keep all the navigation buttons in a single CEWP. I'd also like to trim certain cells out of my table according to the security group membership of the user viewing the page. I realize this can be done with permission levels (using SPSecurityTrimmedControl) but that isn't useful to me since there could be multiple groups that need the same level of access to the CEWP.

Since SharePoint has the ability to do this natively, is there a way to place the same functionality into a content editor webpart and do this at an even finer level?

share|improve this question

4 Answers 4

You can extend the SPSecurityTrimmedControl class to create a custom control that discriminates based on group membership rather than permission level.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.WebControls;
using System.Web.UI;
using Microsoft.SharePoint;

namespace My.Custom.Controls
{
    public class SPGroupTrimmedControl : SPSecurityTrimmedControl
    {
        private List<string> _groups = new List<string>();

        public string Groups
        {
            get
            {
                return string.Join(";", _groups.ToArray());
            }
            set
            {
                _groups.AddRange(value.Split(new char[] { ';' }, System.StringSplitOptions.RemoveEmptyEntries));
            }
        }

        protected override void Render(HtmlTextWriter output)
        {
            if (!string.IsNullOrEmpty(Groups) && UserInGroup())
            {
                base.Render(output);
            }
        }

        private bool UserInGroup()
        {
            SPUser user = SPContext.Current.Web.CurrentUser;

            // show admins all content
            if (user.IsSiteAdmin)
            {
                return true;
            }

            bool isInGroup = false;
            foreach (string groupName in _groups)
            {
                string trimmedGroupName = groupName.Trim();
                int groupID = SPContext.Current.Web.Groups[trimmedGroupName].ID;
                if (SPContext.Current.Web.IsCurrentUserMemberOfGroup(groupID))
                {
                    isInGroup = true;
                    break;
                }
            }
            return isInGroup;
        }
    }
}

and then:

<%@ Register TagPrefix="mycontrols" Namespace="My.Custom.Controls" Assembly="My.Custom.SPGroupTrimmedControl, Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxxxxx" %>

//...

<mycontrols:SPGroupTrimmedControl runat="server" ID="MyGroupTrimmer" Groups="Group 1;Group 2">

    // your trimmed content

</mycontrols:SPGroupTrimmedControl>

ETA: In my own implementation of this, I have a custom method to check if a user is a member of a group rather than using SPWeb.IsCurrentUserMemberOfGroup, so this code, as is, is untested. And I'd just like to point out that the line

int groupID = SPContext.Current.Web.Groups[trimmedGroupName].ID;

could potentially throw an exception if the group is not present on the site, so you might want to throw in a little error handling around that.

share|improve this answer
    
This looks excellent! I will give it a test drive :) Thank you! –  Shrout1 Aug 19 at 16:16

Well, to me it seems this should be pretty easily done with a custom content editor webpart.

  1. Create a new Webpart and have it derive from the Content Editor webpart.
  2. Add a custom action to the rich text editor so that you can use to easily inject an additional wrapper xml element surrounding the pieces of html you want to target.

  3. Use this XML element to hook into your Content XmlElement property of the CEWP in serverside code so that it transforms the result based on the following property: "SPContext.Current.Web.DoesUserHavePermission(SPBasePermissions.ManageLists)" -> Wrote this by hand, check it.

I haven't done this before, but it seems pretty feasible.

Wrapper examples:

<SPSecurityManageLists>
... your code ...
</SPSecurityManageLists>

or

<SPSecurity data-group="Owners" data-webpermissions="ManageLists">
... your code ...
</SPSecurity>
share|improve this answer

This seems pretty similar to what I encountered yesterday. It's not within the CEWP but you could use a script editor that uses css to hide the element and target that to whichever audience(s) of your liking.

Target audience for a stylesheet

share|improve this answer

This seems like it would be natively supported in a Content Query Web Part. It would require some rework on your part to add your links and thumbnails to a list, configure the CQWP slots, and style the CQWP appropriately but at least you know it works and is possible.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.