public static void AddCustomAction(List list, string actionUrl) { ClientContext context = (ClientContext)list.Context; context.Load(list.UserCustomActions); context.ExecuteQuery(); foreach (UserCustomAction uca in list.UserCustomActions) { uca.DeleteObject(); } context.ExecuteQuery(); UserCustomAction action = list.UserCustomActions.Add(); action.Location = "EditControlBlock"; action.Sequence = 10001; action.Title = "Publish this Lessons Learned"; var permissions = new BasePermissions(); permissions.Set(PermissionKind.EditListItems); action.Rights = permissions; action.Url = actionUrl; action.Update(); context.ExecuteQuery(); }
/// <summary> /// Adds or Updates an existing Custom Action [Url] into the [List] Custom Actions /// </summary> /// <param name="list"></param> /// <param name="customactionname"></param> /// <param name="commandUIExtension"></param> public static void AddOrUpdateCustomActionLink(this List list, string customactionname, string commandUIExtension, string location, int sequence) { var sitecustomActions = list.UserCustomActions; list.Context.Load(sitecustomActions); list.Context.ExecuteQueryRetry(); UserCustomAction cssAction = null; if (sitecustomActions.Any(sa => sa.Name == customactionname)) { cssAction = sitecustomActions.FirstOrDefault(fod => fod.Name == customactionname); } else { // Build a custom action cssAction = sitecustomActions.Add(); cssAction.Name = customactionname; } cssAction.Sequence = sequence; cssAction.Location = location; cssAction.CommandUIExtension = commandUIExtension; cssAction.Update(); list.Context.ExecuteQueryRetry(); }
public static void AddAddBirthdayAction(ClientContext ctx) { if (!ctx.Web.ListExists("Person")) { ctx.Web.CreateList(ListTemplateType.GenericList, "Person", false); } List list = ctx.Web.GetListByTitle("Person"); if (!list.FieldExistsById("{A7A83D5A-06C4-4EA0-94D4-831B74B2E077}")) { FieldCreationInformation fieldInfo = new FieldCreationInformation(FieldType.Number); fieldInfo.Id = "{A7A83D5A-06C4-4EA0-94D4-831B74B2E077}".ToGuid(); fieldInfo.InternalName = "CUSTOM_AGE"; fieldInfo.DisplayName = "Current Age"; list.CreateField(fieldInfo); } DelectCustomActionFromList(ctx, "AddAge", list); ctx.Load(ctx.Web, w => w.Url); ctx.ExecuteQuery(); UserCustomAction customAction = list.UserCustomActions.Add(); customAction.Name = "AddAge"; customAction.Location = "EditControlBlock"; customAction.Title = "Have birthday"; customAction.Url = "javascript:window.location = 'https://localhost:44363/home/HaveBirthday?SPHostUrl=" + ctx.Web.Url + "&listId={ListId}&itemid={ItemId}'"; customAction.Update(); ctx.ExecuteQuery(); }
public static void AddorRemoveCustomAction(ClientContext ctx, Uri appUrl, string custactionType) { List list = ctx.Web.GetListByTitle("Customer List"); if (custactionType == "Remove") { ctx.Load(list.UserCustomActions); ctx.ExecuteQuery(); int customActions = list.UserCustomActions.Count(); for (int i = customActions; i > 0; i--) { if (list.UserCustomActions[i - 1].Name == "CustomName") { list.UserCustomActions[i - 1].DeleteObject(); ctx.ExecuteQuery(); } } } else { UserCustomAction action = list.UserCustomActions.Add(); action.Title = "Go to Customer Card"; action.Name = "CustomName"; action.Url = appUrl.Scheme + "://" + appUrl.Authority + "/CustomerCard/Index" + appUrl.Query + "&ListId={ListId}&ListItemId={ItemId}"; action.Location = "EditControlBlock"; action.Sequence = 1; action.Update(); ctx.ExecuteQuery(); } }
private void WireUpJSFileInternal(string scriptSource, string actionName, int sequence) { UserCustomActionCollection userCustomActions = GetCurrentUserCustomActionsDeclaredOnThisWeb(); bool alreadyPresent = false; foreach (UserCustomAction userCustomAction in userCustomActions) { if (!string.IsNullOrEmpty(userCustomAction.Name) && userCustomAction.Name == actionName) { alreadyPresent = true; break; } } if (!alreadyPresent) { // add it UserCustomAction action = userCustomActions.Add(); action.ScriptSrc = scriptSource; action.Location = "ScriptLink"; action.Name = actionName; action.Sequence = sequence; action.Update(); this.hostWebClientContextField.ExecuteQuery(); } }
protected void btnScenario1_Click(object sender, EventArgs e) { var spContext = SharePointContextProvider.Current.GetSharePointContext(Context); using (var clientContext = spContext.CreateUserClientContextForSPHost()) { if (clientContext != null) { Web web = clientContext.Web; List assetLibrary = web.Lists.GetByTitle("Site Assets"); clientContext.Load(assetLibrary, l => l.RootFolder); // Get the path to the file which we are about to deploy string file = System.Web.Hosting.HostingEnvironment.MapPath(string.Format("~/{0}", "CSS/contoso.css")); // Use CSOM to uplaod the file in FileCreationInformation newFile = new FileCreationInformation(); newFile.Content = System.IO.File.ReadAllBytes(file); newFile.Url = "contoso.css"; newFile.Overwrite = true; Microsoft.SharePoint.Client.File uploadFile = assetLibrary.RootFolder.Files.Add(newFile); clientContext.Load(uploadFile); clientContext.ExecuteQuery(); // Now, apply a reference to the CSS URL via a custom action string actionName = "ContosoCSSLink"; // Clean up existing actions that we may have deployed var existingActions = web.UserCustomActions; clientContext.Load(existingActions); // Execute our uploads and initialzie the existingActions collection clientContext.ExecuteQuery(); // Clean up existing custom action with same name, if it exists foreach (var existingAction in existingActions) { if (existingAction.Name.Equals(actionName, StringComparison.InvariantCultureIgnoreCase)) { existingAction.DeleteObject(); } } clientContext.ExecuteQuery(); // Build a custom action to write a link to our new CSS file UserCustomAction cssAction = web.UserCustomActions.Add(); cssAction.Location = "ScriptLink"; cssAction.Sequence = 100; cssAction.ScriptBlock = @"document.write('<link rel=""stylesheet"" href=""" + assetLibrary.RootFolder.ServerRelativeUrl + @"/contoso.css"" />');"; cssAction.Name = actionName; // Apply cssAction.Update(); clientContext.ExecuteQuery(); } } }
protected virtual void ProcessLocalization(UserCustomAction obj, UserCustomActionDefinition definition) { ProcessGenericLocalization(obj, new Dictionary <string, List <ValueForUICulture> > { { "TitleResource", definition.TitleResource }, { "DescriptionResource", definition.DescriptionResource }, { "CommandUIExtensionResource", definition.CommandUIExtensionResource }, }); }
public static void ProvisionArtifactsByCode() { // Create a PnP AuthenticationManager object AuthenticationManager am = new AuthenticationManager(); // Authenticate against SPO with an App-Only access token using (ClientContext context = am.GetAzureADAppOnlyAuthenticatedContext( O365ProjectsAppContext.CurrentSiteUrl, O365ProjectsAppSettings.ClientId, O365ProjectsAppSettings.TenantId, O365ProjectsAppSettings.AppOnlyCertificate)) { Web web = context.Web; List targetLibrary = null; // If the target library does not exist (PnP extension method) if (!web.ListExists(O365ProjectsAppSettings.LibraryTitle)) { // Create it using another PnP extension method targetLibrary = web.CreateList(ListTemplateType.DocumentLibrary, O365ProjectsAppSettings.LibraryTitle, true, true); } else { targetLibrary = web.GetListByTitle(O365ProjectsAppSettings.LibraryTitle); } // If the target library exists if (targetLibrary != null) { // Try to get the user's custom action UserCustomAction customAction = targetLibrary.GetCustomAction(O365ProjectsAppConstants.ECB_Menu_Name); // If it doesn't exist if (customAction == null) { // Add the user custom action to the list customAction = targetLibrary.UserCustomActions.Add(); customAction.Name = O365ProjectsAppConstants.ECB_Menu_Name; customAction.Location = "EditControlBlock"; customAction.Sequence = 100; customAction.Title = "Manage Business Project"; customAction.Url = $"{O365ProjectsAppContext.CurrentAppSiteUrl}Project/?SiteUrl={{SiteUrl}}&ListId={{ListId}}&ItemId={{ItemId}}&ItemUrl={{ItemUrl}}"; } else { // Update the already existing Custom Action customAction.Name = O365ProjectsAppConstants.ECB_Menu_Name; customAction.Location = "EditControlBlock"; customAction.Sequence = 100; customAction.Title = "Manage Business Project"; customAction.Url = $"{O365ProjectsAppContext.CurrentAppSiteUrl}Project/?SiteUrl={{SiteUrl}}&ListId={{ListId}}&ItemId={{ItemId}}&ItemUrl={{ItemUrl}}"; } customAction.Update(); context.ExecuteQueryRetry(); } } }
private void MapCustomAction(UserCustomAction existringAction, UserCustomActionDefinition customAction) { existringAction.Sequence = customAction.Sequence; existringAction.Description = customAction.Description; existringAction.Group = customAction.Group; existringAction.Location = customAction.Location; existringAction.Name = customAction.Name; existringAction.ScriptBlock = customAction.ScriptBlock; existringAction.ScriptSrc = customAction.ScriptSrc; existringAction.Title = customAction.Title; }
private void MapCustomAction(UserCustomAction existringAction, UserCustomActionDefinition customAction) { TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Updating user custom action properties."); existringAction.Sequence = customAction.Sequence; existringAction.Description = customAction.Description; existringAction.Group = customAction.Group; existringAction.Location = customAction.Location; existringAction.Name = customAction.Name; existringAction.ScriptBlock = customAction.ScriptBlock; existringAction.ScriptSrc = customAction.ScriptSrc; existringAction.Title = customAction.Title; existringAction.Url = customAction.Url; if (!string.IsNullOrEmpty(customAction.CommandUIExtension)) { existringAction.CommandUIExtension = customAction.CommandUIExtension; } if (!string.IsNullOrEmpty(customAction.RegistrationId)) { existringAction.RegistrationId = customAction.RegistrationId; } if (!string.IsNullOrEmpty(customAction.RegistrationType)) { // skipping setup for List script // System.NotSupportedException: Setting this property is not supported. A value of List has already been set and cannot be changed. if (customAction.RegistrationType != BuiltInRegistrationTypes.List) { existringAction.RegistrationType = (UserCustomActionRegistrationType) Enum.Parse(typeof(UserCustomActionRegistrationType), customAction.RegistrationType, true); } } var permissions = new BasePermissions(); if (customAction.Rights != null && customAction.Rights.Count > 0) { foreach (var permissionString in customAction.Rights) { permissions.Set((PermissionKind)Enum.Parse(typeof(PermissionKind), permissionString)); } } existringAction.Rights = permissions; ProcessLocalization(existringAction, customAction); }
private void AddJsLink(Microsoft.SharePoint.Client.ClientContext ctx) { Web web = ctx.Web; ctx.Load(web, w => w.UserCustomActions); ctx.ExecuteQuery(); ctx.Load(web, w => w.UserCustomActions, w => w.Url, w => w.AppInstanceId); ctx.ExecuteQuery(); UserCustomAction userCustomAction = web.UserCustomActions.Add(); userCustomAction.Location = "Microsoft.SharePoint.StandardMenu"; userCustomAction.Group = "SiteActions"; BasePermissions perms = new BasePermissions(); perms.Set(PermissionKind.ManageWeb); userCustomAction.Rights = perms; userCustomAction.Sequence = 100; userCustomAction.Title = "Modify Site"; string realm = TokenHelper.GetRealmFromTargetUrl(new Uri(ctx.Url)); string issuerId = WebConfigurationManager.AppSettings.Get("ClientId"); var modifyPageUrl = string.Format("https://{0}/Pages/Modify.aspx?{{StandardTokens}}", Request.Url.Authority); string url = "javascript:LaunchApp('{0}', 'i:0i.t|ms.sp.ext|{1}@{2}','{3}',{{width:300,height:200,title:'Modify Site'}});"; url = string.Format(url, Guid.NewGuid().ToString(), issuerId, realm, modifyPageUrl); userCustomAction.Url = url; userCustomAction.Update(); ctx.ExecuteQuery(); // Remove the entry from the 'Recents' node NavigationNodeCollection nodes = web.Navigation.QuickLaunch; ctx.Load(nodes, n => n.IncludeWithDefaultProperties(c => c.Children)); ctx.ExecuteQuery(); var recent = nodes.Where(x => x.Title == "Recent").FirstOrDefault(); if (recent != null) { var appLink = recent.Children.Where(x => x.Title == "Site Modifier").FirstOrDefault(); if (appLink != null) { appLink.DeleteObject(); } ctx.ExecuteQuery(); } }
/// <summary> /// Handler for teh JS injection pattern /// </summary> /// <param name="clientContext"> /// </param> private void AddJSInjectionToSite(ClientContext clientContext) { // Add JS file to web remotely this.DeployJSInjectionFilesToContextWeb(clientContext); if (!this.CustomActionForJSInjectionAlreadyExists(clientContext)) { UserCustomAction action = clientContext.Web.UserCustomActions.Add(); action.ScriptSrc = "~site/Injection_JS/SubSiteRemoteProvisioningWeb_InjectedJS.js"; action.Location = "ScriptLink"; action.Name = "Injection_JS"; action.Sequence = 1000; action.Update(); clientContext.ExecuteQuery(); } }
private CustomAction CopyUserCustomAction(UserCustomAction userCustomAction, ProvisioningTemplateCreationInformation creationInfo, ProvisioningTemplate template) { var customAction = new CustomAction(); customAction.Description = userCustomAction.Description; customAction.Enabled = true; customAction.Group = userCustomAction.Group; customAction.ImageUrl = userCustomAction.ImageUrl; customAction.Location = userCustomAction.Location; customAction.Name = userCustomAction.Name; customAction.Rights = userCustomAction.Rights; customAction.ScriptBlock = userCustomAction.ScriptBlock; customAction.ScriptSrc = userCustomAction.ScriptSrc; customAction.Sequence = userCustomAction.Sequence; customAction.Title = userCustomAction.Title; customAction.Url = userCustomAction.Url; customAction.RegistrationId = userCustomAction.RegistrationId; customAction.RegistrationType = userCustomAction.RegistrationType; #if !ONPREMISES customAction.ClientSideComponentId = userCustomAction.ClientSideComponentId; customAction.ClientSideComponentProperties = userCustomAction.ClientSideComponentProperties; #endif customAction.CommandUIExtension = !System.String.IsNullOrEmpty(userCustomAction.CommandUIExtension) ? XElement.Parse(userCustomAction.CommandUIExtension) : null; #if !ONPREMISES if (creationInfo.PersistMultiLanguageResources) { var resourceKey = userCustomAction.Name.Replace(" ", "_"); if (UserResourceExtensions.PersistResourceValue(userCustomAction.TitleResource, $"CustomAction_{resourceKey}_Title", template, creationInfo)) { var customActionTitle = $"{{res:CustomAction_{resourceKey}_Title}}"; customAction.Title = customActionTitle; } if (UserResourceExtensions.PersistResourceValue(userCustomAction.DescriptionResource, $"CustomAction_{resourceKey}_Description", template, creationInfo)) { var customActionDescription = $"{{res:CustomAction_{resourceKey}_Description}}"; customAction.Description = customActionDescription; } } #endif return(customAction); }
/// <summary> /// </summary> private void AddScript(ClientContext ctx, Site site, Web web, ref UserActionObject ucaObj) { UserCustomAction uca = null; if (scope.IsEqual("Site Collection")) { uca = site.UserCustomActions.Add(); } else { uca = web.UserCustomActions.Add(); } uca.Location = "ScriptLink"; if (!ucaObj.scriptSrc.IsNull()) { uca.ScriptSrc = ucaObj.scriptSrc; } if (!ucaObj.scriptBlock.IsNull()) { uca.ScriptBlock = ucaObj.scriptBlock; } if (!ucaObj.descr.IsNull()) { uca.Description = ucaObj.descr; } if (!ucaObj.name.IsNull()) { uca.Title = uca.Name = ucaObj.name; } uca.Sequence = ucaObj.seq; uca.Update(); ctx.Load(uca, x => x.Id, x => x.Name); ctx.ExecuteQuery(); ucaObj.id = uca.Id.ToString(); tcout("Action added", ucaObj.id); }
public void AddJsLink(Microsoft.SharePoint.Client.ClientContext ctx) { Web web = ctx.Web; ctx.Load(web, w => w.UserCustomActions); ctx.ExecuteQuery(); ctx.Load(web, w => w.UserCustomActions, w => w.Url, w => w.AppInstanceId); ctx.ExecuteQuery(); UserCustomAction userCustomAction = web.UserCustomActions.Add(); userCustomAction.Location = "Microsoft.SharePoint.StandardMenu"; userCustomAction.Group = "SiteActions"; BasePermissions perms = new BasePermissions(); perms.Set(PermissionKind.ManageWeb); userCustomAction.Rights = perms; userCustomAction.Sequence = 100; userCustomAction.Title = "Say Hello"; string url = "javascript:alert('Hello SharePoint Custom Action!!!');"; userCustomAction.Url = url; userCustomAction.Update(); ctx.ExecuteQuery(); // Remove the entry from the 'Recents' node Microsoft.SharePoint.Client.NavigationNodeCollection nodes = web.Navigation.QuickLaunch; ctx.Load(nodes, n => n.IncludeWithDefaultProperties(c => c.Children)); ctx.ExecuteQuery(); var recent = nodes.Where(x => x.Title == "Recent").FirstOrDefault(); if (recent != null) { var appLink = recent.Children.Where(x => x.Title == "Site Modifier").FirstOrDefault(); if (appLink != null) { appLink.DeleteObject(); } ctx.ExecuteQuery(); } }
private void AddSiteInformationJsLink(Microsoft.SharePoint.Client.ClientContext clientContext) { Web web = clientContext.Web; clientContext.Load(web, w => w.UserCustomActions, w => w.Url, w => w.AppInstanceId); clientContext.ExecuteQuery(); string issuerId = ConfigurationManager.AppSettings.Get("ClientId"); DeleteExistingActions(clientContext, web); UserCustomAction userCustomAction = web.UserCustomActions.Add(); userCustomAction.Location = "Microsoft.SharePoint.StandardMenu"; userCustomAction.Group = "SiteActions"; BasePermissions perms = new BasePermissions(); perms.Set(PermissionKind.ManageWeb); userCustomAction.Rights = perms; userCustomAction.Sequence = 100; userCustomAction.Title = "Site Information"; userCustomAction.Name = "SiteInformationApp"; string realm = TokenHelper.GetRealmFromTargetUrl(new Uri(clientContext.Url)); string host = ""; foreach (Uri u in OperationContext.Current.Host.BaseAddresses) { if (u.Scheme.Equals("https", StringComparison.InvariantCultureIgnoreCase)) { host = u.Authority; } } var appPageUrl = string.Format("https://{0}/Pages/Default.aspx?{{StandardTokens}}", host); string url = "javascript:LaunchApp('{0}', 'i:0i.t|ms.sp.ext|{1}@{2}','{3}', {{width:600,height:400,title:'Site Information'}});"; url = string.Format(url, Guid.NewGuid().ToString(), issuerId, realm, appPageUrl); userCustomAction.Url = url; userCustomAction.Update(); clientContext.ExecuteQuery(); }
private static CustomAction CopyUserCustomAction(UserCustomAction userCustomAction, ProvisioningTemplateCreationInformation creationInfo, ProvisioningTemplate template) { var customAction = new CustomAction { Description = userCustomAction.Description, Enabled = true, Group = userCustomAction.Group, ImageUrl = userCustomAction.ImageUrl, Location = userCustomAction.Location, Name = userCustomAction.Name, Rights = userCustomAction.Rights, ScriptBlock = userCustomAction.ScriptBlock, ScriptSrc = userCustomAction.ScriptSrc, Sequence = userCustomAction.Sequence, Title = userCustomAction.Title, Url = userCustomAction.Url, RegistrationId = userCustomAction.RegistrationId, RegistrationType = userCustomAction.RegistrationType, ClientSideComponentId = userCustomAction.ClientSideComponentId, ClientSideComponentProperties = userCustomAction.ClientSideComponentProperties, CommandUIExtension = !System.String.IsNullOrEmpty(userCustomAction.CommandUIExtension) ? XElement.Parse(userCustomAction.CommandUIExtension) : null }; if (creationInfo.PersistMultiLanguageResources) { var resourceKey = userCustomAction.Name.Replace(" ", "_"); if (UserResourceExtensions.PersistResourceValue(userCustomAction.TitleResource, $"CustomAction_{resourceKey}_Title", template, creationInfo)) { var customActionTitle = $"{{res:CustomAction_{resourceKey}_Title}}"; customAction.Title = customActionTitle; } if (!string.IsNullOrWhiteSpace(userCustomAction.Description) && UserResourceExtensions.PersistResourceValue(userCustomAction.DescriptionResource, $"CustomAction_{resourceKey}_Description", template, creationInfo)) { var customActionDescription = $"{{res:CustomAction_{resourceKey}_Description}}"; customAction.Description = customActionDescription; } } return(customAction); }
private CustomActionCreator GetCreatorFromUserCustomAction(UserCustomAction userCustomAction) { var newCreator = new CustomActionCreator { Title = userCustomAction.Title, Description = userCustomAction.Description, Group = userCustomAction.Group, ImageUrl = userCustomAction.ImageUrl, Location = userCustomAction.Location, RegistrationId = userCustomAction.RegistrationId, RegistrationType = userCustomAction.RegistrationType, ScriptBlock = userCustomAction.ScriptBlock, ScriptSrc = userCustomAction.ScriptSrc, Sequence = userCustomAction.Sequence, Url = userCustomAction.Url, CommandUIExtension = userCustomAction.CommandUIExtension }; return(newCreator); }
static void Main(string[] args) { using (ClientContext ctx = LogInAsUser()) { //TaxonomyHelper.CreateTaxonomyFavColor(ctx); //SetupHelper.SetUp(ctx); ctx.Load(ctx.Web, w => w.Url); ctx.ExecuteQuery(); UserCustomAction action = ctx.Web.GetListByTitle("Schools").UserCustomActions.Add();; string url = "https://localhost:44358/"; action.Location = "EditControlBlock"; action.Sequence = 1; action.Name = "SchoolAction"; action.Title = "school card"; action.Url = "https://localhost:44358/Home/School?itemId={ItemId}&SPHostUrl=" + ctx.Web.Url; action.Update(); ctx.ExecuteQuery(); } }
private CustomAction CopyUserCustomAction(UserCustomAction userCustomAction) { var customAction = new CustomAction(); customAction.Description = userCustomAction.Description; customAction.Enabled = true; customAction.Group = userCustomAction.Group; customAction.ImageUrl = userCustomAction.ImageUrl; customAction.Location = userCustomAction.Location; customAction.Name = userCustomAction.Name; customAction.Rights = userCustomAction.Rights; customAction.ScriptBlock = userCustomAction.ScriptBlock; customAction.ScriptSrc = userCustomAction.ScriptSrc; customAction.Sequence = userCustomAction.Sequence; customAction.Title = userCustomAction.Title; customAction.Url = userCustomAction.Url; customAction.RegistrationId = userCustomAction.RegistrationId; customAction.RegistrationType = userCustomAction.RegistrationType; customAction.CommandUIExtension = userCustomAction.CommandUIExtension; return(customAction); }
/// <summary> /// Adds or Updates an existing Custom Action [ScriptBlock] into the [Site] Custom Actions /// </summary> /// <param name="site"></param> /// <param name="customactionname"></param> /// <param name="customActionBlock"></param> /// <param name="sequence"></param> public static void AddOrUpdateCustomActionLinkBlock(this Site site, string customactionname, string customActionBlock, int sequence) { var sitecustomActions = site.GetCustomActions(); UserCustomAction cssAction = null; if (site.CustomActionExists(customactionname)) { cssAction = sitecustomActions.FirstOrDefault(fod => fod.Name == customactionname); } else { // Build a custom action to write a link to our new CSS file cssAction = site.UserCustomActions.Add(); cssAction.Name = customactionname; cssAction.Location = "ScriptLink"; } cssAction.Sequence = sequence; cssAction.ScriptBlock = customActionBlock; cssAction.Update(); site.Context.ExecuteQueryRetry(); }
static void CreateScriptLinks() { // Register ScriptLink for jQuery UserCustomAction customAction1 = site.UserCustomActions.Add(); customAction1.Title = "jQuery"; customAction1.Location = "ScriptLink"; customAction1.ScriptSrc = "~SiteCollection/CPT/scripts/jquery.js"; customAction1.Sequence = 10; customAction1.Update(); // Register ScriptLink for custom javascript file UserCustomAction customAction2 = site.UserCustomActions.Add(); customAction2.Title = "CustomUserActions"; customAction2.Location = "ScriptLink"; customAction2.ScriptSrc = "~SiteCollection/CPT/scripts/CustomUserActions.js"; customAction2.Sequence = 11; customAction2.Update(); clientContext.ExecuteQuery(); }
public static void AddCustomActionToListItem(ClientContext ctx) { if (!ctx.Web.ListExists("FakeList")) { ctx.Web.CreateList(ListTemplateType.GenericList, "FakeList", false); } List list = ctx.Web.GetListByTitle("FakeList"); DelectCustomActionFromList(ctx, "FakeCustomAction", list); ctx.Load(ctx.Web, w => w.Url); ctx.ExecuteQuery(); UserCustomAction userAction = list.UserCustomActions.Add(); userAction.Name = "FakeCustomAction"; userAction.Location = "EditControlBlock"; userAction.Title = "Fake Custom Action"; userAction.Url = "https://localhost:44363/home/about?SPHostUrl=" + ctx.Web.Url + "&listId={ListId}&itemid={ItemId}"; userAction.Update(); ctx.ExecuteQuery(); }
private CustomAction CopyUserCustomAction(UserCustomAction userCustomAction) { var customAction = new CustomAction(); customAction.Description = userCustomAction.Description; customAction.Enabled = true; customAction.Group = userCustomAction.Group; customAction.ImageUrl = userCustomAction.ImageUrl; customAction.Location = userCustomAction.Location; customAction.Name = userCustomAction.Name; customAction.Rights = userCustomAction.Rights; customAction.ScriptBlock = userCustomAction.ScriptBlock; customAction.ScriptSrc = userCustomAction.ScriptSrc; customAction.Sequence = userCustomAction.Sequence; customAction.Title = userCustomAction.Title; customAction.Url = userCustomAction.Url; customAction.RegistrationId = userCustomAction.RegistrationId; customAction.RegistrationType = userCustomAction.RegistrationType; customAction.CommandUIExtension = !System.String.IsNullOrEmpty(userCustomAction.CommandUIExtension) ? XElement.Parse(userCustomAction.CommandUIExtension) : null; return(customAction); }
/// <summary> /// Generic handler for custom action entries /// </summary> /// <param name="clientContext"> /// The client context /// </param> /// <param name="siteTemplate"> /// XML structure for the template /// </param> private void DeployCustomActions(ClientContext clientContext, XElement siteTemplate) { XElement customActionsToDeploy = siteTemplate.Element("CustomActions"); if (customActionsToDeploy != null) { foreach (XElement customAction in customActionsToDeploy.Elements()) { if (this.CustomActionAlreadyExists(clientContext, customAction.Attribute("Name").Value)) { continue; } UserCustomAction action = clientContext.Web.UserCustomActions.Add(); action.ScriptSrc = customAction.Attribute("ScriptSrc").Value; action.Location = customAction.Attribute("Location").Value; action.Name = customAction.Attribute("Name").Value; action.Sequence = 1000; action.Update(); clientContext.ExecuteQuery(); } } }
/// <summary> /// </summary> private void AddScript(ClientContext ctx, Site site, ref UserActionObject userActionObj) { UserCustomAction spUserCustomAction = site.UserCustomActions.Add(); spUserCustomAction.Location = "ScriptLink"; if (!userActionObj.scriptSrc.IsNull()) { spUserCustomAction.ScriptSrc = userActionObj.scriptSrc; } if (!userActionObj.scriptBlock.IsNull()) { spUserCustomAction.ScriptBlock = userActionObj.scriptBlock; } if (!userActionObj.descr.IsNull()) { spUserCustomAction.Description = userActionObj.descr; } if (!userActionObj.name.IsNull()) { spUserCustomAction.Name = userActionObj.name; } spUserCustomAction.Sequence = userActionObj.seq; spUserCustomAction.Update(); ctx.Load(spUserCustomAction, x => x.Id, x => x.Name); ctx.ExecuteQuery(); userActionObj.id = spUserCustomAction.Id.ToString(); tcout("Action added", userActionObj.id); }
/// <summary> /// Adds or Updates an existing Custom Action [Url] into the [List] Custom Actions /// </summary> /// <param name="list"></param> /// <param name="customactionname"></param> /// <param name="customactionurl"></param> /// <param name="title"></param> /// <param name="description"></param> /// <param name="location"></param> /// <param name="sequence">(default) 10000</param> /// <param name="groupName">(optional) adds custom group</param> public static void AddOrUpdateCustomActionLink(this List list, SPCustomActionList action) { var sitecustomActions = list.UserCustomActions; list.Context.Load(sitecustomActions); list.Context.ExecuteQueryRetry(); UserCustomAction cssAction = null; if (sitecustomActions.Any(sa => sa.Name == action.name)) { cssAction = sitecustomActions.FirstOrDefault(fod => fod.Name == action.name); } else { // Build a custom action cssAction = sitecustomActions.Add(); cssAction.Name = action.name; } cssAction.Sequence = action.sequence; cssAction.Url = action.Url; cssAction.Description = action.Description; cssAction.Location = action.Location; cssAction.Title = action.Title; if (!string.IsNullOrEmpty(action.ImageUrl)) { cssAction.ImageUrl = action.ImageUrl; } if (!string.IsNullOrEmpty(action.Group)) { cssAction.Group = action.Group; } cssAction.Update(); list.Context.ExecuteQueryRetry(); }
private static void ChangeCustomActionRegistration() { using (var clientContext = sPContext.CreateUserClientContextForSPHost()) { var query = from action in clientContext.Web.UserCustomActions where action.Name == "6601a902-f458-4757-9000-09f23eaa5386.AddEmployeeToCorpDB" select action; IEnumerable <UserCustomAction> matchingActions = clientContext.LoadQuery(query); clientContext.ExecuteQuery(); UserCustomAction webScopedEmployeeAction = matchingActions.Single(); var queryForList = from list in clientContext.Web.Lists where list.Title == "Local Employees" select list; IEnumerable <List> matchingLists = clientContext.LoadQuery(queryForList); clientContext.ExecuteQuery(); List employeeList = matchingLists.First(); var listActions = employeeList.UserCustomActions; clientContext.Load(listActions); listActions.Clear(); var listScopedEmployeeAction = listActions.Add(); listScopedEmployeeAction.Title = webScopedEmployeeAction.Title; listScopedEmployeeAction.Location = webScopedEmployeeAction.Location; listScopedEmployeeAction.Sequence = webScopedEmployeeAction.Sequence; listScopedEmployeeAction.CommandUIExtension = webScopedEmployeeAction.CommandUIExtension; listScopedEmployeeAction.Update(); webScopedEmployeeAction.DeleteObject(); clientContext.ExecuteQuery(); } }
private void MapCustomAction(UserCustomAction existringAction, UserCustomActionDefinition customAction) { TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Updating user custom action properties."); existringAction.Sequence = customAction.Sequence; existringAction.Description = customAction.Description; existringAction.Group = customAction.Group; existringAction.Location = customAction.Location; existringAction.Name = customAction.Name; existringAction.ScriptBlock = customAction.ScriptBlock; existringAction.ScriptSrc = customAction.ScriptSrc; existringAction.Title = customAction.Title; existringAction.Url = customAction.Url; if (!string.IsNullOrEmpty(customAction.CommandUIExtension)) existringAction.CommandUIExtension = customAction.CommandUIExtension; if (!string.IsNullOrEmpty(customAction.RegistrationId)) existringAction.RegistrationId = customAction.RegistrationId; if (!string.IsNullOrEmpty(customAction.RegistrationType)) { // skipping setup for List script // System.NotSupportedException: Setting this property is not supported. A value of List has already been set and cannot be changed. if (customAction.RegistrationType != BuiltInRegistrationTypes.List) { existringAction.RegistrationType = (UserCustomActionRegistrationType) Enum.Parse(typeof(UserCustomActionRegistrationType), customAction.RegistrationType, true); } } var permissions = new BasePermissions(); if (customAction.Rights != null && customAction.Rights.Count > 0) { foreach (var permissionString in customAction.Rights) permissions.Set((PermissionKind)Enum.Parse(typeof(PermissionKind), permissionString)); } existringAction.Rights = permissions; }
private CustomAction CopyUserCustomAction(UserCustomAction userCustomAction) { var customAction = new CustomAction(); customAction.Description = userCustomAction.Description; customAction.Enabled = true; customAction.Group = userCustomAction.Group; customAction.ImageUrl = userCustomAction.ImageUrl; customAction.Location = userCustomAction.Location; customAction.Name = userCustomAction.Name; customAction.Rights = userCustomAction.Rights; customAction.ScriptBlock = userCustomAction.ScriptBlock; customAction.ScriptSrc = userCustomAction.ScriptSrc; customAction.Sequence = userCustomAction.Sequence; customAction.Title = userCustomAction.Title; customAction.Url = userCustomAction.Url; customAction.RegistrationId = userCustomAction.RegistrationId; customAction.RegistrationType = userCustomAction.RegistrationType; customAction.CommandUIExtension = !System.String.IsNullOrEmpty(userCustomAction.CommandUIExtension) ? XElement.Parse(userCustomAction.CommandUIExtension) : null; return customAction; }
private CustomAction CopyUserCustomAction(UserCustomAction userCustomAction, ProvisioningTemplateCreationInformation creationInfo, ProvisioningTemplate template) { var customAction = new CustomAction(); customAction.Description = userCustomAction.Description; customAction.Enabled = true; customAction.Group = userCustomAction.Group; customAction.ImageUrl = userCustomAction.ImageUrl; customAction.Location = userCustomAction.Location; customAction.Name = userCustomAction.Name; customAction.Rights = userCustomAction.Rights; customAction.ScriptBlock = userCustomAction.ScriptBlock; customAction.ScriptSrc = userCustomAction.ScriptSrc; customAction.Sequence = userCustomAction.Sequence; customAction.Title = userCustomAction.Title; customAction.Url = userCustomAction.Url; customAction.RegistrationId = userCustomAction.RegistrationId; customAction.RegistrationType = userCustomAction.RegistrationType; customAction.CommandUIExtension = !System.String.IsNullOrEmpty(userCustomAction.CommandUIExtension) ? XElement.Parse(userCustomAction.CommandUIExtension) : null; #if !ONPREMISES if (creationInfo.PersistMultiLanguageResources) { var resourceKey = userCustomAction.Name.Replace(" ", "_"); if (UserResourceExtensions.PersistResourceValue(userCustomAction.TitleResource, string.Format("CustomAction_{0}_Title", resourceKey), template, creationInfo)) { var customActionTitle = string.Format("{{res:CustomAction_{0}_Title}}", resourceKey); customAction.Title = customActionTitle; } if (UserResourceExtensions.PersistResourceValue(userCustomAction.DescriptionResource, string.Format("CustomAction_{0}_Description", resourceKey), template, creationInfo)) { var customActionDescription = string.Format("{{res:CustomAction_{0}_Description}}", resourceKey); customAction.Description = customActionDescription; } } #endif return customAction; }
private static void SetCustomActionResourceValues(TokenParser parser, CustomAction customAction, UserCustomAction uca) { if (uca != null) { bool isDirty = false; #if !ONPREMISES if (!string.IsNullOrEmpty(customAction.Title) && customAction.Title.ContainsResourceToken()) { if (uca.TitleResource.SetUserResourceValue(customAction.Title, parser)) { isDirty = true; } } if (!string.IsNullOrEmpty(customAction.Description) && customAction.Description.ContainsResourceToken()) { if (uca.DescriptionResource.SetUserResourceValue(customAction.Description, parser)) { isDirty = true; } } #endif if (isDirty) { uca.Update(); uca.Context.ExecuteQueryRetry(); } } }
private CustomAction CopyUserCustomAction(UserCustomAction userCustomAction) { var customAction = new CustomAction(); customAction.Description = userCustomAction.Description; customAction.Enabled = true; customAction.Group = userCustomAction.Group; customAction.ImageUrl = userCustomAction.ImageUrl; customAction.Location = userCustomAction.Location; customAction.Name = userCustomAction.Name; customAction.Rights = userCustomAction.Rights; customAction.ScriptBlock = userCustomAction.ScriptBlock; customAction.ScriptSrc = userCustomAction.ScriptSrc; customAction.Sequence = userCustomAction.Sequence; customAction.Title = userCustomAction.Title; customAction.Url = userCustomAction.Url; customAction.RegistrationId = userCustomAction.RegistrationId; customAction.RegistrationType = userCustomAction.RegistrationType; customAction.CommandUIExtension = userCustomAction.CommandUIExtension; return customAction; }
public SPOUserCustomAction(UserCustomAction userCustomAction) { _userCustomAction = userCustomAction; }
internal static void UpdateCustomAction(TokenParser parser, PnPMonitoredScope scope, CustomAction customAction, UserCustomAction existingCustomAction) { var isDirty = false; // Otherwise we update it if (customAction.CommandUIExtension != null) { if (existingCustomAction.CommandUIExtension != parser.ParseString(customAction.CommandUIExtension.ToString())) { scope.LogPropertyUpdate("CommandUIExtension"); existingCustomAction.CommandUIExtension = parser.ParseString(customAction.CommandUIExtension.ToString()); isDirty = true; } } else { // Required to allow for a delta action to blank out the CommandUIExtension attribute existingCustomAction.CommandUIExtension = null; } if (existingCustomAction.Description != customAction.Description) { scope.LogPropertyUpdate("Description"); existingCustomAction.Description = customAction.Description; isDirty = true; } #if !ONPREMISES if (customAction.Description.ContainsResourceToken()) { if (existingCustomAction.DescriptionResource.SetUserResourceValue(customAction.Description, parser)) { isDirty = true; } } #endif if (existingCustomAction.Group != customAction.Group) { scope.LogPropertyUpdate("Group"); existingCustomAction.Group = customAction.Group; isDirty = true; } if (existingCustomAction.ImageUrl != parser.ParseString(customAction.ImageUrl)) { scope.LogPropertyUpdate("ImageUrl"); existingCustomAction.ImageUrl = parser.ParseString(customAction.ImageUrl); isDirty = true; } if (existingCustomAction.Location != customAction.Location) { scope.LogPropertyUpdate("Location"); existingCustomAction.Location = customAction.Location; isDirty = true; } if (existingCustomAction.RegistrationId != customAction.RegistrationId) { scope.LogPropertyUpdate("RegistrationId"); existingCustomAction.RegistrationId = customAction.RegistrationId; isDirty = true; } if (existingCustomAction.RegistrationType != customAction.RegistrationType) { scope.LogPropertyUpdate("RegistrationType"); existingCustomAction.RegistrationType = customAction.RegistrationType; isDirty = true; } if (existingCustomAction.ScriptBlock != parser.ParseString(customAction.ScriptBlock)) { scope.LogPropertyUpdate("ScriptBlock"); existingCustomAction.ScriptBlock = parser.ParseString(customAction.ScriptBlock); isDirty = true; } if (existingCustomAction.ScriptSrc != parser.ParseString(customAction.ScriptSrc, "~site", "~sitecollection")) { scope.LogPropertyUpdate("ScriptSrc"); existingCustomAction.ScriptSrc = parser.ParseString(customAction.ScriptSrc, "~site", "~sitecollection"); isDirty = true; } if (existingCustomAction.Sequence != customAction.Sequence) { scope.LogPropertyUpdate("Sequence"); existingCustomAction.Sequence = customAction.Sequence; isDirty = true; } if (existingCustomAction.Title != parser.ParseString(customAction.Title)) { scope.LogPropertyUpdate("Title"); existingCustomAction.Title = parser.ParseString(customAction.Title); isDirty = true; } #if !ONPREMISES if (customAction.Title.ContainsResourceToken()) { if (existingCustomAction.TitleResource.SetUserResourceValue(customAction.Title, parser)) { isDirty = true; } } #endif if (existingCustomAction.Url != parser.ParseString(customAction.Url)) { scope.LogPropertyUpdate("Url"); existingCustomAction.Url = parser.ParseString(customAction.Url); isDirty = true; } if (isDirty) { existingCustomAction.Update(); existingCustomAction.Context.ExecuteQueryRetry(); } }
public static string GetRegistrationType(this UserCustomAction action) { return(action.RegistrationType.ToString()); }
private void ProvisionCustomActionImplementation(object parent, List <CustomAction> customActions, TokenParser parser, PnPMonitoredScope scope) { Web web = null; Site site = null; if (parent is Site) { site = parent as Site; // Switch parser context; parser.Rebase(site.RootWeb); } else { web = parent as Web; // Switch parser context parser.Rebase(web); } foreach (var customAction in customActions) { var caExists = false; if (site != null) { caExists = site.CustomActionExists(customAction.Name); } else { caExists = web.CustomActionExists(customAction.Name); } if (!caExists) { var customActionEntity = new CustomActionEntity() { CommandUIExtension = customAction.CommandUIExtension != null?parser.ParseString(customAction.CommandUIExtension.ToString()) : string.Empty, Description = customAction.Description, Group = customAction.Group, ImageUrl = parser.ParseString(customAction.ImageUrl), Location = customAction.Location, Name = customAction.Name, RegistrationId = customAction.RegistrationId, RegistrationType = customAction.RegistrationType, Remove = customAction.Remove, Rights = customAction.Rights, ScriptBlock = parser.ParseString(customAction.ScriptBlock), ScriptSrc = parser.ParseString(customAction.ScriptSrc, "~site", "~sitecollection"), Sequence = customAction.Sequence, Title = customAction.Title, Url = parser.ParseString(customAction.Url) }; if (site != null) { scope.LogDebug(CoreResources.Provisioning_ObjectHandlers_CustomActions_Adding_custom_action___0___to_scope_Site, customActionEntity.Name); site.AddCustomAction(customActionEntity); } else { scope.LogDebug(CoreResources.Provisioning_ObjectHandlers_CustomActions_Adding_custom_action___0___to_scope_Web, customActionEntity.Name); web.AddCustomAction(customActionEntity); } } else { UserCustomAction existingCustomAction = null; if (site != null) { existingCustomAction = site.GetCustomActions().FirstOrDefault(c => c.Name == customAction.Name); } else { existingCustomAction = web.GetCustomActions().FirstOrDefault(c => c.Name == customAction.Name); } if (existingCustomAction != null) { var isDirty = false; if (customAction.CommandUIExtension != null) { if (existingCustomAction.CommandUIExtension != parser.ParseString(customAction.CommandUIExtension.ToString())) { scope.LogPropertyUpdate("CommandUIExtension"); existingCustomAction.CommandUIExtension = parser.ParseString(customAction.CommandUIExtension.ToString()); isDirty = true; } } if (existingCustomAction.Description != customAction.Description) { scope.LogPropertyUpdate("Description"); existingCustomAction.Description = customAction.Description; isDirty = true; } if (existingCustomAction.Group != customAction.Group) { scope.LogPropertyUpdate("Group"); existingCustomAction.Group = customAction.Group; isDirty = true; } if (existingCustomAction.ImageUrl != parser.ParseString(customAction.ImageUrl)) { scope.LogPropertyUpdate("ImageUrl"); existingCustomAction.ImageUrl = parser.ParseString(customAction.ImageUrl); isDirty = true; } if (existingCustomAction.Location != customAction.Location) { scope.LogPropertyUpdate("Location"); existingCustomAction.Location = customAction.Location; isDirty = true; } if (existingCustomAction.RegistrationId != customAction.RegistrationId) { scope.LogPropertyUpdate("RegistrationId"); existingCustomAction.RegistrationId = customAction.RegistrationId; isDirty = true; } if (existingCustomAction.RegistrationType != customAction.RegistrationType) { scope.LogPropertyUpdate("RegistrationType"); existingCustomAction.RegistrationType = customAction.RegistrationType; isDirty = true; } if (existingCustomAction.ScriptBlock != parser.ParseString(customAction.ScriptBlock)) { scope.LogPropertyUpdate("ScriptBlock"); existingCustomAction.ScriptBlock = parser.ParseString(customAction.ScriptBlock); isDirty = true; } if (existingCustomAction.ScriptSrc != parser.ParseString(customAction.ScriptSrc, "~site", "~sitecollection")) { scope.LogPropertyUpdate("ScriptSrc"); existingCustomAction.ScriptSrc = parser.ParseString(customAction.ScriptSrc, "~site", "~sitecollection"); isDirty = true; } if (existingCustomAction.Title != parser.ParseString(customAction.Title)) { scope.LogPropertyUpdate("Title"); existingCustomAction.Title = parser.ParseString(customAction.Title); isDirty = true; } if (existingCustomAction.Url != parser.ParseString(customAction.Url)) { scope.LogPropertyUpdate("Url"); existingCustomAction.Url = parser.ParseString(customAction.Url); isDirty = true; } if (isDirty) { existingCustomAction.Update(); existingCustomAction.Context.ExecuteQueryRetry(); } } } } }
protected virtual void ProcessLocalization(UserCustomAction obj, UserCustomActionDefinition definition) { ProcessGenericLocalization(obj, new Dictionary<string, List<ValueForUICulture>> { { "TitleResource", definition.TitleResource }, { "DescriptionResource", definition.DescriptionResource }, { "CommandUIExtensionResource", definition.CommandUIExtensionResource }, }); }