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();