Tag Archives: CAML

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;

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">
           <?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">
              <Height />
              <Width />
              <DetailLink />
              <HelpLink />
              <PartImageSmall />
              <MissingAssembly>Det går inte att importera den här webbdelen.</MissingAssembly>
              <IsIncludedFilter />
              <Assembly>Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
              <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;
              <PartStorage xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor" />

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.


Add Edit Control Block (ECB) menu to custom column

Sometimes, as a developer, I tend to overthink things, not looking for the most simple of solutions for a problem. This was the case when I wanted to change the url of the linkTitle field on a custom list I made, to the url stored in a custom column in the same list. My first solution included jQuery and SharePoint javascript to get all the listItem objects for the items shown in a view, and for each of those, run queries to retrieve the data I needed. This solutions was poor because it consumed unnecessary resources and was not completely stable.

After being almost completely finished with the solution I thought that what I really wanted to achieve was adding the Edit Control Block (Short: ECB, see picture below) menu to my custom column, which really has the same effect (for me) as changing the url for the linkTitle column. A couple of searches and I found that this is done by simply adding a property to my custom column in my content type and list definition:


(Picture of ECB menu in swedish)

So instead of using like 40 lines of javascript code I could use a very small line of CAML. Very neat indeed. =)

So if you want to create a custom column with the ECB menu, you can define it like this (My example is for a hyperlink column):

<Field ID="{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}" 

CAML Query Example: Including GroupBy

Just to remember how to write these things.

                <FieldRef Name="myFieldName" />
                <Value Type="DateTime">
                  <Today OffsetDays="6"/>
                <FieldRef Name="myFieldName2"/>
                <Value Type="Choice">myFieldValue</Value>
            Collapse ="TRUE"
            GroupLimit ="100">
            <FieldRef Name="myFieldName">
            <FieldRef Name="MBLEventStart">