Search This Blog

Thursday, January 21, 2016

Retrieve items from a particular folder of a list or Library SP 2013

Lists and Libraries are the commonly used data storage components is a SharePoint portal. With data growing huge in the lists, site administrators usually create folders to make the data organized. We use the 'ViewAttribute' of the SPQuery object to fetch all the items from the folders and sub folders. Below is a sample code snippet
using (SPWeb spWeb = SPContext.Current.Site.AllWebs["Reports"]) { SPList spList = spWeb.Lists.TryGetList("ManageReports"); SPQuery spQuery = new SPQuery(); //Setting the ViewAttribute to "Scope='Recursive'" //fetches all items from the list, //ie., also the items within the folders spQuery.ViewAttributes = "Scope=\"Recursive\""; SPListItemCollection items = spList.GetItems(spQuery); }
If the 'ViewAttribute' is not specified for the SPQuery object, then only those items which are in the root folder (ie., the root level of the list) are returned. So, we need to specify the scope in ViewAttribute property to get the items from all the folders (if any) of a list.
GETTING ITEMS FROM A SPECIFIC FOLDER
In some scenarios we might need to fetch only the items within a sepcific folder. For this we can use the 'Folder' property of SPQuery class. Prepare the SPQuery object with the required CAML query, set the RowLimit and other required settings. And then set the 'Folder' property to a Folder object which represents the folder instance from where we need the items. Refer the below code snippet
using (SPWeb spWeb = SPContext.Current.Site.AllWebs["Reports"]) { SPList spList = spWeb.Lists.TryGetList("ManageReports"); SPQuery spQuery = new SPQuery(); //Getting the folder object from the list SPFolder folder = spList.RootFolder.SubFolders["FolderName"]; //Set the Folder property spQuery.Folder = folder; SPListItemCollection items = spList.GetItems(spQuery); //Now the items collection will have the items only from the given Folder }

Tuesday, January 12, 2016

Add “Link to a Document” to a Document Library

I threw this together for a customer, and figured I’d blog it, as it’s generally fairly useful to remember how to do this again in the future.  The steps below help to guide you through the process of adding the “Link to a Document” content type to a document library.
1) Go into the Site Actions -> View All Site Content, and find the library you want to change
image
2) Go into the Library Settings for that library
image
3) Go into “Advanced Settings”, and choose “Allow management of content type” – Select “Yes”, when done, click “OK”
image
4) A new option will now be available on the Library Settings page, “Content Types”.  Choose “Add from existing site content types”.
clip_image002[5]
5) From the list on the left, find “Link to a Document” and add it to the list on the right.
image
6) You will now have 2 content types, including “Link to a Document”.  Exit the settings page.
clip_image012
7) In your library, you will now have the ability to add a link to a document as a library “item”.  Choose this, and let’s try it out.
image
8) Basically what this allows you to do is to create a name / value pair, where the “Document Name” is what will show up in the library as the “name” and the URL is basically any reference that you want it to be (it could even link to google.com – for all SharePoint cares).  What you’ll likely want this to be is a URL to a document in the other site’s document library.
clip_image016
9) Finally, in case you didn’t know, the easiest way to get the Document URL for a file is to go to the file and choose “Email a link”.  This will pop open a new email message, but it’s the easiest means of getting the URL to a document.  Just steal the URL from the email and throw the email away.  Finally, paste that value into the dialog above, and you’re all set!
image
image
That should get you everything you need.