Thursday, June 23, 2016

How to exclude all files within folders in Document Library through SharePoint Object model

While working on one of the requirement in my project , I need to show the files in Root Folder of Document library only not from child folders within it.

Through "Folder" option in default view, I can only set to show the folder or show files without folder.

But through code we will get all the files in root folder as well as in child folder also.

To resolve this, you need to use Scope="FilesOnly" for the ViewAttributes property of the SPQuery.

 SPList list = _oWeb.Lists["Library Name"];
 SPQuery spQuery = new SPQuery();
 spQuery.ViewAttributes = "Scope=\"FilesOnly\"";
 SPListItemCollection items = list.GetItems(spQuery);

Activate hyperlink on a custom column in document library/list in Default View

You can do it by setting LinkToItem="True" attribute in the default view for list or document library.

You can use SharePoint Designer for this, open your list or library and then your view and make change like shown below:

     <FieldRef Name="DocIcon"/>
     <FieldRef Name="LinkFilename"/>
     <FieldRef Name="Modified"/>
     <FieldRef Name="Editor"/>
     <FieldRef Name="FileSizeDisplay"/>
     <FieldRef Name="CustomLink" LinkToItem="TRUE"/>

If you want to set any specific URL, then you can create a "calculated column" by using a formula with existing column and use this new calculated column in above example.

=CONCATENATE("http://YourDomain/sites/SiteCollection/Lists/ListName/Forms/<<any form name>>.aspx?ID=",ID)