I'm working on a rather large SharePoint project that's going to contain helper classes for a variety of item levels (sites, webs, lists, items, etc').
When writing code I'm expecting that the same site and authentication will span across multiple contexts (as in you might use a list helper, and then an item helper with the same credentials). For this reason I've built a credentials class to hold onto those.
Also since site names are going to be used in every helper I've built a "base helper class to hold the information.
Here are my current classes:
My authentication class
/// <summary>
/// SharePoint Class for doing quick authentication tests with a SharePoint Online Server
/// </summary>
public class SPAuthentication
{
/// <summary>
/// Initialize authentication class with connection information
/// </summary>
/// <param name="username">The <paramref name="username"/> (@domain or @onmicrosoft.com) for connecting to SharePoint Online</param>
/// <param name="password"></param>
/// <exception cref="CryptographicException">An error occurred when encrypting the parameter <paramref name="password"/> into a secure string</exception>
/// <exception cref="ObjectDisposedException">This secure string has already been disposed.</exception>
/// <exception cref="ArgumentOutOfRangeException">Performing this operation would make the length of this secure string greater than 65536 characters.</exception>
public SPAuthentication(string username, string password)
{
var securePassword = new SecureString();
// use password input to create a secure string for use with SharePoint Online
foreach (var c in password.ToCharArray()) securePassword.AppendChar(c);
Credentials = new SharePointOnlineCredentials(username, securePassword);
}
/// <summary>
/// Container to hold SharePoint credentials after instantiating class. This prevents the user having to repeatably fetch the user/pass and recreate a credentials class
/// </summary>
public readonly SharePointOnlineCredentials Credentials;
}
My base helper class
/// <summary>
/// A base helper class for use with other SharePoint related helper.
/// </summary>
public class SPBaseHelper
{
/// <summary>
/// Backing field for SiteUrl property
/// </summary>
private string _spSiteUrl;
/// <summary>
/// Property for storing the SharePoint site to be used for operations
/// </summary>
public string SiteUrl
{
get { return _spSiteUrl; }
set
{
_spSiteUrl = value;
_spSiteUrl = _spSiteUrl?.Replace(@"\", @"/");
}
}
/// <summary>
/// Current SHarePoint authentication information.
/// </summary>
internal SharePointOnlineCredentials SPCredentials;
}
The foundation of my first helper class (lists)
/// <summary>
/// A helper class for use with SharePoint lists using the client object model
/// </summary>
public class SPListHelper : SPBaseHelper
{
/// <summary>
/// Instantiate new helper class with
/// </summary>
/// <param name="auth">SharePoint Class for doing quick authentication tests with a SharePoint Online Server</param>
public SPListHelper(SPAuthentication auth)
{
SPCredentials = auth.Credentials;
}
/// <summary>
/// Perform a test connection to a SharePoint Online Site
/// </summary>
/// <returns>Returns the site name that the class was opened with</returns>
public string TestConnect()
{
if (SiteUrl == null) return "No site specified.";
using (var ctx = new ClientContext(SiteUrl))
{
ctx.Credentials = SPCredentials;
var web = ctx.Web;
ctx.Load(web);
ctx.ExecuteQuery();
return web.Title;
}
}
}
Is this going to be too difficult for those that follow? Is there a better approach I could change to this early on into a project, what general improvements could I make?