Programmatically Upload and Delete documents from SharePoint library

Posted on Updated on

Documents can be uploaded or deleted from SharePoint library using C# without using SharePoint dlls.
There may arise cases when you want to performs upload and delete documents to and from SharePoint document libraries using C# but you don’t have or want to involve SharePoint dlls to perform this action.
System.Net.WebClient  and System.Net.WebRequest comes in handy in such situation.
These can be used in tandem to achieve the required goal.
// Upload document to SharePoint library
System.Net.WebClient sharePointLibrary = new System.Net.WebClient();
sharePointLibrary.Credentials = new System.Net.NetworkCredential(“SharePointUser“,”SharePointUserPassword“);
byte[] response = sharePointLibrary.UploadFile(Path.Combine(“SharePointURI“, “ResultantFileName“), “PUT“, Path.Combine(“TestLocationofFileToUpload“, “NameOfTheFileToBeUploaded“));
// Delete document from SharePoint library
var request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(“FullSharePointURIofDocument“);
request.Timeout = System.Threading.Timeout.Infinite;
request.Credentials = new System.Net.NetworkCredential(“SharePointUser“,”SharePointUserPassword“);
request.Method = “DELETE“;
var response = (System.Net.HttpWebResponse)request.GetResponse();

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s