Tuesday 24 January 2017

What you should know before Development of SharePoint?

Although Architecture for SharePoint to access list or file of Document Library define in this image:

but in code you have to access first site then Open web.
Get SharePoint List Using C# code:
In this tutorial we will learn how to get Document Library files by traversing SharePoint Architecture.
SharePoint Architecture to access any list:
SecurityPrevillages-> Site(ByProvidingURL) ->Web -> List (like DocumentLaibrary) -> Get Files under Document Library

Steps:
1: Create Console Application using C# etc then add this library "using Microsoft.SharePoint".
2: Get the path of site which library files you want access in your code like (string baseUrl = "http://sp2013:81/sites/sis/";)
3:  Security Check if you are running this utility on same server where SharePoint is deployed. Get Security check by adding this line SPSecurity.RunWithElevatedPrivileges(delegate() {  //YourCOde });
4: Then Get site where you want some changes or want to get something from existing applications like Document Library.
5: Get all uploaded documents on document library by passing library name as a parameter in   SPDocumentLibrary lib = (SPDocumentLibrary)web.Lists[LaibraryName];
6: using Lib.RootFolder to get all files existing in this folder.
This is Generic Function written in C# to access all documents from a specific Library:
public static bool getAllDocuments(String LaibraryName)
{
Console.WriteLine("getAllDocuments debug, START");
bool isOK = false;
string baseUrl = "http://sp2013:81/sites/sis/";
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(baseUrl))
{
using (SPWeb web = site.OpenWeb())
{
SPDocumentLibrary lib = (SPDocumentLibrary)web.Lists[LaibraryName]; //Get All documents with name
//SPDocumentLibrary lib = (SPDocumentLibrary)web.Lists["Client Incoming Correspondence"]; //Get All documents with name

IEnumerable<SPFile> allFiles = ExploreFolder(lib.RootFolder);
foreach (SPFile file in allFiles)
{
Console.WriteLine("getAllDocuments debug, File Name : " + file.GetType());
string filename = file.Name;
Console.WriteLine("getAllDocuments debug, File CharSetName : " + file.CharSetName);
Console.WriteLine("getAllDocuments debug, File SourceLeafName : " + file.Title);
string strFileExtension = new FileInfo(file.Name).Extension;
string strFileUrl = file.Url;
Console.WriteLine("strFileUrl : " + strFileUrl);

}

}
}
});
}
catch (Exception e)
{
Console.WriteLine("getAllDocuments debug, " + e.Message);
isOK = true;
}
Console.WriteLine("getAllDocuments debug, END");
return isOK;
}
By using this approach you can access any list. Thanks for reading this blog if you need any help feel free to comment here. Thanks


No comments:

Post a Comment