Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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!

share|improve this question
add comment

2 Answers 2

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.

share|improve this answer
add comment

This should get you started on uploading a file in SharePoint with the Client Object Model:

http://blogs.msdn.com/b/sridhara/archive/2010/03/12/uploading-files-using-client-object-model-in-sharepoint-2010.aspx

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.