I am trying to add new web part to the SharePoint page using Managed Client Object Model that will display a list. I can't seem to be able to figure out the code of the web part's XML that is responsible for list infromation.

Here is what I have created so far:

File page;
LimitedWebPartManager webPartManager;
WebPartDefinition wpd;

page = clientContext.Web.GetFileByServerRelativeUrl("/SitePages/Home.aspx");

webPartManager = page.GetLimitedWebPartManager(PersonalizationScope.Shared);
wpd = webPartManager.ImportWebPart(webPartXml); // This XML string is what I need to figure out!
webPartManager.AddWebPart(wpd.WebPart, zone, order);

page.CheckIn(string.Empty, CheckinType.MajorCheckIn);

For example, here is the XML string that successfully creates a Content Editor Web Part:

<?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>My web part added through Client Object Model</Title>
  <Description>It displays the custom content quite well!</Description>
  <Height />
  <Width />
  <DetailLink />
  <HelpLink />
  <PartImageSmall />
  <MissingAssembly>Cannot import this Web Part.</MissingAssembly>
  <IsIncludedFilter />
  <Assembly>Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=94de0004b6e3fcc5</Assembly>
  <ContentLink xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor" />
  <Content xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor">
    <![CDATA[This is my test text!
             <DIV>Blah blah blah</DIV>
             <DIV>And another blah</DIV>]]>
  <PartStorage xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor" />

I need one similar to it which would create a List Web Part. The code after the <Assembly> tag should be adjusted for SharePoint List Web Parts. Does anyone know what the XML should look like? Thanks!

3 Answers 3


The XML is going to be web part specific. While there are some commonalities (ZoneID, PartOrder, AllowRemove, etc)... these are the properties of the web part. Since SP provides those common properties (you find these in the "edit web part" window), you might believe they are required by all. They are not. If not specified, the default values will be used.

Regarding the specifics. Assembly and TypeName define the exact web part you want to use. As you noticed, the Content Editor Web Part includes a Content element, which includes XML encoded contents. Alternatively, the list view web part (at least in 2007) included XML encoded CAML which was used to specify the query, and generate the rendered output.

Regarding the ListView WP and its content.

The easiest approach would be to create the view you want, then open SPD and examine the page's markup. SPD will show you the web part's XML definition, in its entirety.

Regarding the CAML rendering.

be aware that editing CAML is NOT a pleasant task. Basically, SharePoint includes several templates, which can be specified when editing a view, and changing its style... each style has its own CAML.

The list of styles comes from the VWSTYLES.XML file in the SharePoint hive. This file is NOT extensible. The CAML includes a reference to the style ID, as identified by VWSTYLES.XML.

It IS possible to create a custom style. Adjust the CAML as necessary, and be sure NOT to reference any style ID. You should be aware that the entire view will be READ ONLY (no editing the fields, filters, grouping, etc). This is due to the removal of the styleID reference. If you do NOT remove the reference to the style ID, any changes to the view (adding a field, filter, etc) will OVERWRITE your custom CAML with that from VWSTYLES.

IF you REALLY need the custom CAML to be reusable, customizable, etc... you can add it to the VWSTYLES file (new ID of course)... you should be ABSOLUTELY aware, that this is NOT a supported customization. There is no guarantee that your changes will be preserved when a patch or service pack is applied. There is NO way that the changes will be propagated across servers, if a new one is added. This is 100% MANUAL effort!

I've done this... once... and only once... on SP2007... check my blog link: http://sbrickey.com/Tech/Blog/Post/CAML_Query_for_SharePoint_ListView_web_part

It should also be noted, that SP2010 has made a conscious effort to abandon CAML for its ListView web parts, and instead focus on XSLT for the rendering.


I've found a brief snippet that'll give you the first couple of lines, which is a starting point at least: How to add a list view web part to onet.xml or your elements manifest

<WebPart xmlns="http://schemas.microsoft.com/WebPart/v2">
    <Assembly>Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>

Unfortunately I can only find definitions within a view element, so the list and view to be used are implied.

SharePoint Manager also appears to have trouble exporting the XML from existing List View web parts, so that path of investigation is also troublesome.

  • Thanks for the update! I'll play around and see if I get the hang of it.
    – Boris
    Commented Nov 22, 2011 at 14:48

In case someone wonders, this is enough for the default behaviour the same as when added through GUI (replace #LISTGUID# with your list.Id.ToString() ):

<?xml version="1.0" encoding="utf-8" ?>
  <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
      <type name="Microsoft.SharePoint.WebPartPages.XsltListViewWebPart, Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
        <importErrorMessage>Tätä WWW-osaa ei voi tuoda.</importErrorMessage>
            <property name="ListId" type="System.Guid, mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089">#LISTGUID#</property>

  • And in order to use list url instead of guid: <property name="ListUrl" type="string">Lists/TestList</property>
    – thomius
    Commented Feb 20, 2014 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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