Instantly share code, notes, and snippets.
AsishP/Add Extension using SharePoint CSOM
Last active Sep 12, 2018
private string appJsonInfo; | |
private AppDeclaration Apps; | |
// A class to hold JSON converted objects for Apps | |
private class AppDeclaration | |
{ | |
public string appName; | |
public string appId; | |
public string clientsideId; | |
} | |
// Declare and create a Json Object for all Apps | |
public void DeclareAppIds | |
{ | |
appJsonInfo = "[{appId: \"xxxxx-xxxx-xxxx-xxxx-xxxxxxxx\", appName: \"testApp\", clientsideId: \"xxxxxx-xxxx-xxxx-xxxx-xxxxxxxx\"}, " + | |
"{appId: \"xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx\", appName: \"testApp1\", clientsideId: \"xxxxxx-xxxx-xxxx-xxxxx-xxxxxx\"}, " + | |
"{appId: \"xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx\", appName: \"testApp2\", clientsideId: \"xxxxxx-xxxx-xxxx-xxxxx-xxxxxx\"}]"; | |
Apps = JsonConvert.DeserializeObject<List<AppDeclaration>>(appJsonInfo); | |
} | |
public void AddExtensions(string listURL) | |
{ | |
AppDeclaration App = appConfiguration.Apps.Find(app => app.appName.Contains("<app name>")); | |
Guid AppID = new Guid(App.clientsideId); | |
string AppName = App.appName; | |
using (ClientContext context = new ClientContext(<siteUrl>)) | |
{ | |
if (AppID != Guid.Empty) | |
{ | |
context.Credentials = new SharePointOnlineCredentials(UserName, SecurePass); | |
List list = context.Web.GetListByUrl(<list URL>); | |
log.Info("Finding and Adding Extenstion to " + list.Title); | |
context.Load(list); | |
context.Load(list, t => t.UserCustomActions); | |
context.ExecuteQuery(); | |
UserCustomActionCollection listExt = list.UserCustomActions; | |
bool AppPresent = false; | |
foreach (UserCustomAction customAcc in listExt) | |
{ | |
if (customAcc.ClientSideComponentId == AppID) | |
{ | |
tenancyAppPresent = true; | |
continue; | |
} | |
} | |
if (!tenancyAppPresent) | |
{ | |
UserCustomAction customAction = list.UserCustomActions.Add(); | |
customAction.ClientSideComponentId = AppID; | |
customAction.Location = "ClientSideExtension.ListViewCommandSet"; | |
customAction.Name = AppName; | |
customAction.Title = AppName; | |
customAction.Update(); | |
context.ExecuteQuery(); | |
} | |
} | |
else | |
throw new Exception("System Error - App cannot be added as no App ID found"); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment