I am implementing a new authentication method according to the version of SharePoint in my .NET code, which may vary between the URL. However, I am unsure of how to declare an empty ClientContext
variable.
Below is my code snippet:
Uri lUri = new Uri(pSiteUrl);
//To declare an empty ClientContext variable here//
if (pSiteUrl.StartsWith(ConfigurationManager.AppSettings["New_URL"]))
{
ClientContext lClientContext = SPOClientContext.GetAuthenticatedContext(pSiteUrl);
}
else
{
ClientContext lClientContext = new ClientContext(pSiteUrl);
}
//lClientContext does not exists if it is not initialised outside the if-else statement above
Folder lRootFolder = lClientContext.Web.GetFolderByServerRelativeUrl(lUri.AbsolutePath + pFolderPath);
I am not sure if this will be of any help but I will try to do my best :).
The ClientContext constructor must have some parameter -> Link
So what You could do is just create not initialized ClientContext and then check if it was not null... something like this should do (based on You code example):
... just please remember that the ClientContext should always be disposed at the end
maybe some cleaner option would be something like this (that way You will not need to think about the Dispose() at the end)