0

Is it possible to add a Document Library as a webpart to a sharepoint page

0

The Solution is posting in the same question here:

Add Document Library XsltListViewWebPart using CSOM or web services

WebPart XML:

<?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=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
          <importErrorMessage>Cannot import this Web Part.</importErrorMessage>
        </metaData>
        <data>
          <properties>
            <property name="ListUrl" type="string">MyLibrary</property>
            <property name="MissingAssembly" type="string">Cannot import this Web Part.</property>
          </properties>
        </data>
      </webPart>
    </webParts>

CSOM Code:

    WebPart importingWebPart = mgr.ImportWebPart(webPartXml).WebPart; // take webPartXml from above
    WebPartDefinition wpDefinition = mgr.AddWebPart(importingWebPart, "Top", 1);
    mgr.Context.Load(wpDefinition,
        d => d.Id); // Id of the hidden view which gets automatically created
    mgr.Context.ExecuteQuery();
    var viewId = wpDefinition.Id;
    
    List list = web.Lists.GetByTitle("MyLibrary");
    View view = list.Views.GetById(viewId);
    view.ViewFields.RemoveAll();
    view.ViewFields.Add("Title");
    view.ViewQuery = "<Where><Eq><FieldRef Name=\"Title\" /><Value Type=\"Text\">Something Here</Value></Eq></Where>";
    view.RowLimit = 10;
    web.Context.ExecuteQuery();
8
  • I believe the most appropriate use of this CAML Query string is following the Value Type for Files, like this: view.ViewQuery = "<Where><Eq><FieldRef Name=\"FileLeafRef\" /><Value Type=\"File\">some_file_name.extension</Value></Eq></Where>"; Jan 29 at 18:45
  • @AntonioLeonardo, yes. no problem as Title may be duplicated, but file name is unique, the CAML Query is optional, the key point to add library as web part is adding the xml definition in the page.
    – Jerry
    Jan 30 at 3:26
  • i/m able to add the doc library as a webpart on a page. but my actual requirement is to show items of a specific subfolder as a webpart on the page and i'm creating a custom view and adding that view as a webpart..adding a web part is working but the custom view shows 0 items
    – Hari babu
    Feb 1 at 11:44
  • this is query i/m using while creating the view
    – Hari babu
    Feb 1 at 11:44
  • creationInfo.Query = "<Where><Contains><FieldRef Name='FileDirRef'/><Value Type='Text'>Shared Documents/4 – Internal Reviews</Value></Contains></Where>";
    – Hari babu
    Feb 1 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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