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 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");
page.CheckOut();

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>
  <FrameType>Default</FrameType>
  <Description>It displays the custom content quite well!</Description>
  <IsIncluded>true</IsIncluded>
  <ZoneID>Left</ZoneID>
  <PartOrder>0</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>Cannot import this Web Part.</MissingAssembly>
  <PartImageLarge>/_layouts/images/mscontl.gif</PartImageLarge>
  <IsIncludedFilter />
  <Assembly>Microsoft.SharePoint, Version=13.0.0.0, Culture=neutral, PublicKeyToken=94de0004b6e3fcc5</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">
    <![CDATA[This is my test text!
             <DIV>&nbsp;</DIV>
             <DIV>Blah blah blah</DIV>
             <DIV>&nbsp;</DIV>
             <DIV>And another blah</DIV>]]>
  </Content>
  <PartStorage xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor" />
</WebPart>

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!

share|improve this question
add comment

3 Answers 3

up vote 1 down vote accepted

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.

share|improve this answer
add comment

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=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
    <TypeName>Microsoft.SharePoint.WebPartPages.ListViewWebPart</TypeName>
    <Title>Title</Title>
</WebPart>

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.

share|improve this answer
    
Thanks for the update! I'll play around and see if I get the hang of it. –  Boris Nov 22 '11 at 14:48
add comment

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" ?>
<webParts>
  <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
    <metaData>
      <type name="Microsoft.SharePoint.WebPartPages.XsltListViewWebPart, Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
        <importErrorMessage>Tätä WWW-osaa ei voi tuoda.</importErrorMessage>
    </metaData>
    <data>
        <properties>
            <property name="ListId" type="System.Guid, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">#LISTGUID#</property>
        </properties>
    </data>
</webPart>

share|improve this answer
    
And in order to use list url instead of guid: <property name="ListUrl" type="string">Lists/TestList</property> –  thomius Feb 20 at 19:45
add comment

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.