Tag Archives: WebPart

How to provision a ContentEditorWebPart (CEWP) with predefined content to a page using a module

I did this a long time ago, then when I recently had to do it again, I couldn’t fid any Resources online describing how to do it. Simply adding text to the <Content> tags of the <AllUsersWebPart> declaration didn’t do it. The reason is simple I Think. The content in a CEWP is html, and you need encoded HTML tags for it to show. Otherwise the content will simply be ignored (This is my own theory at least. I haven’t investigated much).

So knowing that you can add any html content. Here is how you add an image for example:

<Content xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor"> 
    ​&lt;img alt=&quot;&quot; src=&quot;/PublishingImages/image-sample.png&quot;&gt;
</Content>

The easiest way to get encoded HTML is to use an Encoder, like this one. Simply paste your html and press encode. Then paste the results inside the <Content> tag for the CEWP. My full code example can be found below. This is to be put inside the File element in a Module:

<AllUsersWebPart WebPartZoneID="Right" WebPartOrder="1">
        <![CDATA[
           <?xml version="1.0" encoding="utf-8"?>
            <WebPart xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.microsoft.com/WebPart/v2">
              <Title>MyTitle</Title>
              <FrameType>None</FrameType>
              <Description>MyDescription</Description>
              <IsIncluded>true</IsIncluded>
              <ZoneID>Right</ZoneID>
              <PartOrder>4</PartOrder>
              <FrameState>Normal</FrameState>
              <Height />
              <Width />
              <AllowRemove>true</AllowRemove>
              <AllowZoneChange>true</AllowZoneChange>
              <AllowMinimize>true</AllowMinimize>
              <AllowConnect>true</AllowConnect>
              <AllowEdit>true</AllowEdit>
              <AllowHide>true</AllowHide>
              <IsVisible>true</IsVisible>
              <DetailLink />
              <HelpLink />
              <HelpMode>Modeless</HelpMode>
              <Dir>Default</Dir>
              <PartImageSmall />
              <MissingAssembly>Det går inte att importera den här webbdelen.</MissingAssembly>
              <PartImageLarge>/_layouts/images/mscontl.gif</PartImageLarge>
              <IsIncludedFilter />
              <Assembly>Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
              <TypeName>Microsoft.SharePoint.WebPartPages.ContentEditorWebPart</TypeName>
              <ContentLink xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor" />
              <Content xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor"> 
                ​&lt;img alt=&quot;&quot; src=&quot;/PublishingImages/image-sample.png&quot;&gt;
              </Content>
              <PartStorage xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor" />
              </WebPart>
              ]]>

This works in SP2010 and 2013. Just change the assembly version to 14 or 15 depending on which you are using. I presume it works the same in 2007 also, but I haven’t tried.

Advertisements

Hide/Show WebPart depending on current users affiliation

I was very surprised when I started to search for a solution to the task of only showing a webPart I made for users belonging to certain permission groups and found out that there wasn’t any really simple way of doing this. I thought that this is something people should want to do all the time. I found a couple of blogs suggesting creating an audience and hiding the WebPart for that audience. This would not actually affect security however, and felt like a bad solution. Of course you could break and change the permission for the webPart in the webPart gallery, but still not good enough.

Instead I found an easier way of doing this in the WebPart class itself. I’m not sure this is a good solution or not, and there are possibly tons of faults in it, but it works at least.

Basically I do two things:

  • Check if the current user belongs to the correct groups.
  • Set the WebPart to hidden if user does not belong to the right groups.

This is how I check that the current user belongs to the correct groups (If you want to check the permission levels instead, a bit more code is needed. Check this link for example):

private bool UserHasAccess()
        {
            bool hasAccess = false;
            try
            {
                using (SPSite site = new SPSite(SPContext.Current.Site.Url))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        foreach (SPGroup userGroup in web.CurrentUser.Groups)
                        {
                            if (userGroup.Name == "Group1" ||
                                userGroup.Name == "Group2" ||
                                userGroup.Name == "Group3" )
                            {
                                hasAccess = true;
                            }
                            else
                            {
                                hasAccess = false;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return hasAccess;
        }

Then I just show or hide the webPart:

private void ManageVisibility()
        {
            this.Hidden = !UserHasAccess();
        }

Finally, call the method ManageVisibility from the overridden CreateChildControls in your WebPart and voilá! The webPart will be shown for users belonging to the selected groups only. As I said before, this does not check permission levels, only group affiliation.