Getting Client Context in SharePoint online and 2013 customization
public class TestClientContext
{
string envName = Convert.ToString(ConfigurationManager.AppSettings["EnvName"]);
string UserName = Convert.ToString(ConfigurationManager.AppSettings["UserName"]);
string Password = Convert.ToString(ConfigurationManager.AppSettings["Password"]);
public ClientContext GetClientContext(HttpContext httpContext)
{
var securePassword = new SecureString();
//Convert string to secure string
foreach (char c in Password)
securePassword.AppendChar(c);
securePassword.MakeReadOnly();
string envSiteUrl = string.Empty;
ClientContext clientContext = null;
switch (envName)
{
case "DEV":
envSiteUrl = Convert.ToString(ConfigurationManager.AppSettings["SiteURL"]);
clientContext = new ClientContext(envSiteUrl);
clientContext.Credentials = new SharePointOnlineCredentials(UserName, securePassword);
break;
default:
var spContext = SharePointContextProvider.Current.GetSharePointContext(httpContext);
clientContext = spContext.CreateUserClientContextForSPHost();
break;
}
return clientContext;
}
public ClientContext GetClientContext()
{
HttpContext httpContext = HttpContext.Current;
return GetClientContext(httpContext);
}
public string GetSPHostURL(HttpRequest request)
{
string spHostUrl = string.Empty;
switch (envName)
{
case "DEV":
spHostUrl = string.Format("http://{0}", request.Url.Authority);
break;
default:
spHostUrl = request.QueryString["SPHostUrl"].ToString();
break;
}
return spHostUrl;
}
}
No comments:
Post a Comment