The following is code that demonstrates using HubKey's Client-side API for SharePoint. This API wraps many of SharePoint Server 2007's Web Services (Lists Web Service, Webs Web Service, etc.) to provide a familiar object model that can be used on remote client machines without having to process XML requests and responses. In this case the code from the MSDN article How to: Upload a File to a SharePoint Site from a Local Folder was copied verbatim, and it compiles and runs successfully on a client computer, even though the SharePoint DLLs are not referenced or installed.
Update: You can download a demo copy of the API - click here for information.
Update II: The API now includes a demo application - see this post for details.
using System;
using System.IO;
// Using HubKey.Web.Services.SharePoint in place of Microsoft.SharePoint
using HubKey.Web.Services.SharePoint;
namespace HubKey.DevelopmentHole
{
class Program
{
static void Main(string[] args)
{
Program program = new Program();
program.RunDemo();
}
public void RunDemo()
{
string webUrl = "http://remoteserver";
SPSite site = new SPSite(webUrl);
SPWeb web = site.OpenWeb();
//create a new document library
Guid id = web.Lists.Add("Temp Doc Lib", "A temp document library.", SPListTemplateType.DocumentLibrary);
SPList list = web.Lists[id];
//add a field
list.Fields.Add("Upload Source", SPFieldType.Text, false);
list.Update();
string fileUrl = webUrl + "/Temp Doc Lib/New Folder/test.txt";
string localFileName = @"D:\Temp\test.txt";
string localFileContents = "A temp file uploaded by HubKey's SharePoint client-side API.";
File.WriteAllText(localFileName, localFileContents);
//Upload the file using the API code from 'How to: Upload a File to a SharePoint Site from a Local Folder'
//see http://msdn.microsoft.com/en-us/library/ms454491.aspx
UploadFile(localFileName, fileUrl);
//Check out
SPFile file = web.GetFile(fileUrl);
file.CheckOut();
//update the title and new upload source field by using the file's SPListItem property...
file.Item["Title"] = "A temp title";
file.Item["Upload Source"] = "HubKey's SharePoint client-side API.";
file.Item.Update();
//Check in
file.CheckIn("Checked in by HubKey's SharePoint client-side API");
//Get the file contents
string serverFileContents = web.GetFileAsString(fileUrl);
System.Diagnostics.Debug.Assert(string.Equals(localFileContents, serverFileContents));
//Tidy up
list.Delete();
}
public void UploadFile(string srcUrl, string destUrl)
{
if (!File.Exists(srcUrl))
{
throw new ArgumentException(String.Format("{0} does not exist",
srcUrl), "srcUrl");
}
SPWeb site = new SPSite(destUrl).OpenWeb();
FileStream fStream = File.OpenRead(srcUrl);
byte[] contents = new byte[fStream.Length];
fStream.Read(contents, 0, (int)fStream.Length);
fStream.Close();
EnsureParentFolder(site, destUrl);
SPFile file = site.Files.Add(destUrl, contents);
SPFolder folder = file.ParentFolder;
}
public string EnsureParentFolder(SPWeb parentSite, string destinUrl)
{
destinUrl = parentSite.GetFile(destinUrl).Url;
int index = destinUrl.LastIndexOf("/");
string parentFolderUrl = string.Empty;
if (index > -1)
{
parentFolderUrl = destinUrl.Substring(0, index);
SPFolder parentFolder
= parentSite.GetFolder(parentFolderUrl);
if (!parentFolder.Exists)
{
SPFolder currentFolder = parentSite.RootFolder;
foreach (string folder in parentFolderUrl.Split('/'))
{
currentFolder = currentFolder.SubFolders.Add(folder);
}
}
}
return parentFolderUrl;
}
}
}