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

1 Answer 1


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" ?>
      <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
          <type name="Microsoft.SharePoint.WebPartPages.XsltListViewWebPart, Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
          <importErrorMessage>Cannot import this Web Part.</importErrorMessage>
            <property name="ListUrl" type="string">MyLibrary</property>
            <property name="MissingAssembly" type="string">Cannot import this Web Part.</property>

CSOM Code:

    WebPart importingWebPart = mgr.ImportWebPart(webPartXml).WebPart; // take webPartXml from above
    WebPartDefinition wpDefinition = mgr.AddWebPart(importingWebPart, "Top", 1);
        d => d.Id); // Id of the hidden view which gets automatically created
    var viewId = wpDefinition.Id;
    List list = web.Lists.GetByTitle("MyLibrary");
    View view = list.Views.GetById(viewId);
    view.ViewQuery = "<Where><Eq><FieldRef Name=\"Title\" /><Value Type=\"Text\">Something Here</Value></Eq></Where>";
    view.RowLimit = 10;
  • 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, 2021 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, 2021 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, 2021 at 11:44
  • this is query i/m using while creating the view
    – Hari babu
    Feb 1, 2021 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, 2021 at 11: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.