0
votes

My client has requested to upload files to his share point server from and external application. So I have designed a windows application and used Microsoft.sharepoint.dll supplied by client and used the following code for upload.

 Public Function UploadFileToSharepoint(ByVal pstrSourceFilePath As String, ByVal pstrTargeSPURL As String) As Boolean

    If Not File.Exists(pstrSourceFilePath) Then

        Throw New ArgumentException(String.Format("{0} does not exist", pstrSourceFilePath), "srcUrl")

    End If

    Dim site As SPWeb = New SPSite(pstrTargeSPURL).OpenWeb()

    Dim fStream As FileStream = File.OpenRead(pstrSourceFilePath)
    Dim contents(CInt(fStream.Length)) As Byte
    fStream.Read(contents, 0, CInt(fStream.Length))
    fStream.Close()

    EnsureParentFolder(site, pstrTargeSPURL)

    site.Files.Add(pstrTargeSPURL, contents)

    Return True
End Function

I am able to compile it but while running the application I am getting an error like "Could not load or found an assembly "Microsoft.Sharepoint.Library.dll".

My question: Is it possible to develop an application to create a folder structure and upload the file to share point server without having the share point installed on the machine but having only the share point dll's?

Suggest me a way to carry out this kind of development. In future my application will run on a machine where share point server is not installed.

Rupesh

2

2 Answers

3
votes

Yes, you can do that using Client Object Model - just reference Microsoft.SharePoint.Client in your project. Here's how to do it in C#, VB.net shouldn't be much different.

ClientContext context = new ClientContext("http://mydomain");
Web web = context.Web;
FileCreationInformation newFile = new FileCreationInformation();
newFile.Content = System.IO.File.ReadAllBytes(@"C:\MyFile.docx");
newFile.Url = "MyFile.docx"; 
List docs = web.Lists.GetByTitle("Documents");
Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(newFile);
context.Load(uploadFile);
context.ExecuteQuery();
0
votes

You should look into using the SharePoint Client-Side Object model (CSOM). That will allow you to interact with SharePoint from a client.

More info here --> http://msdn.microsoft.com/en-us/library/office/ee535451(v=office.14).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1