I'm a beginner with SP COM. I can't find any samples about this online.
Could anybody tell me how I should proceed to add or remove a file from a document library in SP 2010 with the Client Object Model?
Thanks!
To add a document to a document library in VB.Net:
Imports SP = Microsoft.SharePoint.Client
Public Sub LoadFileToLibrary()
Dim clientContext As SP.ClientContext = GetYourContext()
Dim fileContent As Byte() = GetYourByteArrayOfYourFilesContents()
Dim fileCreationInfo As SP.FileCreationInformation = New SP.FileCreationInformation()
fileCreationInfo.Content = fileContent
fileCreationInfo.Overwrite = True
fileCreationInfo.Url = "Your full destination URL here including file name"
Dim file As SP.File
Dim folder As SP.Folder = list.RootFolder
file = folder.Files.Add(fileCreationInfo)
clientContext.ExecuteQuery()
End Sub
Same classes and everything in C# too. Should be easy enough to translate this snippet.
To delete a document, you can use the same code you would use to delete a list item (because a document library is just a special type of list). You should read How to: Create, Update, and Delete List Items.
Hope this helps.
This should get you started on uploading a file in SharePoint with the Client Object Model: