SharePoint 2013 supports API methods given below to perform basic operations like Create, Update, Read and Delete .
- Server Side Object Model (SSOM).
- Client Object Model (CSOM).
- JavaScript Object Model (JSOM).
- REST.
Server Side Object Model is pure .NET code but here, one needs to use Client Object Model to perform all the basic operations in SharePoint 2013 to achieve all this.
Prerequisites to perform these operations are given below.
- Visual Studio 2012.
- SharePoint 2013.
Before starting to develop the code, we have to add DLL assembly reference.
- SharePoint.Client.Runtime.dll.
- SharePoint.Client.dll.
Add the line given below in your namespace.
using Microsoft.SharePoint.Client;
Now, we are going to see how to create a list in SharePoint 2013 programmatically.
Use
Web.GetFileByServerRelativeUrl Method to return the file object located at the specified Server-relative URL.
Code
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text.RegularExpressions;
- using Microsoft.SharePoint.Client;
- namespace Rextester {
- public class Program {
- public static void Main(string[] args) {
- ClientContext context = new ClientContext("http://gowtham.sharepoint.com");
- Web web = context.Web;
- Microsoft.SharePoint.Client.File file;
- if(TryGetFileByServerRelativeUrl(Web,"/documents/test.docx",out file))
- {
-
- }
- }
- }
-
- public static bool TryGetFileByServerRelativeUrl(Web web, string serverRelativeUrl,out Microsoft.SharePoint.Client.File file)
- {
- var ctx = web.Context;
- try{
- file = web.GetFileByServerRelativeUrl(serverRelativeUrl);
- ctx.Load(file);
- ctx.ExecuteQuery();
- return true;
- }
- catch(Microsoft.SharePoint.Client.ServerException ex){
- if (ex.ServerErrorTypeName == "System.IO.FileNotFoundException")
- {
- file = null;
- return false;
- }
- else
- throw;
- }
- }
- }
Conclusion
Was my blog helpful? If yes, please let me know and if not, please explain what was confusing or missing.
I’ll use your feedback to double-check the facts, add info and update this blog.