I have been getting more and more requests to integrate Visual Studio Tools for Office solutions with SharePoint. As such I find myself connecting to SharePoint Lists and grabbing files and uploading files, etc. The following set of functions are a small subset (but most the most generic), that I can provide for now. The primary function here is CopyToSharePoint. This function does exactly what it sounds like. You provide the URL to the site, the List name you want to copy to, the local path and filename of the file you want to upload.
This does NOT work with Office 365 SharePoint lists. Those require a special project you can download from MSDN to get you the proper SharePoint context for the Microsoft Cloud. The following example only works with on-premises SharePoint installs.
NOTE: I will be working on cleaning up all my SharePoint integration code so it is generic (detached from its specific solutions), and attach it here as a full solution.
[sourcecode language=”csharp”]
/// <summary>
/// Copies the given file to the SharePoint site
/// </summary>
/// <param name="PstrSite">The URL to the site: http://sharepoint/site </param>
/// <param name="PstrList">The name of the list: Documents </param>
/// <param name="PstrLocalPath">The local path of the file to be copied: c:\test\test.docx</param>
/// <param name="PstrLocalName">The local name of the file: text.docx</param>
public static void CopyToSharePoint(string PstrSite, string PstrList, string PstrLocalPath, string PstrLocalName)
{
try
{
SPContext LobjContext = GetSharePointContext();
SPList LobjList = GetSharePointList(PstrSite, PstrList);
SPFolder LobjFolder = LobjList.RootFolder;
LobjContext.Load(LobjFolder);
LobjContext.ExecuteQuery();
string LstrRelativeUrl = LobjFolder.ServerRelativeUrl + "/" + PstrLocalName;
// copy the temp file
using (var LobjFs = new FileInfo(PstrLocalPath).OpenRead())
{
SP.File.SaveBinaryDirect(Common.GobjClientContext, LstrRelativeUrl, LobjFs, true);
}
}
catch (Exception PobjEx)
{
MessageBox.Show(PobjEx.ToString());
}
}
/// <summary>
/// Get the SharePoint context
/// </summary>
/// <returns></returns>
public static SP.Context GetSharePointContext(string PstrSite)
{
// get the current context
SPClientContext LobjContext = new SPClientContext(PstrSite);
// do we have a cached credential?
if (MobjCredential == null)
{
// no – ask the user to sign in or use current
CredentialsForm LobjCreds = new CredentialsForm();
if (LobjCreds.ShowDialog() == DialogResult.OK)
{
if (LobjCreds.UseCurrent)
{
MobjCredential = CredentialCache.DefaultNetworkCredentials;
}
else
{
MobjCredential = new NetworkCredential(LobjCreds.UserName,
LobjCreds.Password,
LobjCreds.Domain);
}
}
else
{
PbolUserCancel = true;
return null; // cancel
}
}
LobjContext.Credentials = MobjCredential;
return LobjContext;
}
/// <summary>
/// Access the list
/// </summary>
/// <returns></returns>
public static SP.List GetSharePointList(string PstrSite, string PstrTitle)
{
try
{
SPList LobjList = null;
SPContext LobjContext = GetSharePointContext(PstrSite);
if (LobjContext != null)
{
if (!string.IsNullOrEmpty(PstrTitle))
{
SPListCollection LobjLists = LobjContext.Web.Lists;
LobjContext.Load(LobjLists);
LobjContext.ExecuteQuery();
foreach (SPList LobjItem in LobjLists)
{
if (LobjItem.Title == PstrTitle)
{
LobjList = LobjItem;
break;
}
}
// done
return LobjList;
}
else
{
return null;
}
}
else
{
return null;
}
}
catch (Exception PobjEx)
{
MessageBox.Show(PobjEx.ToString());
return null;
}
}
[/sourcecode]