コード例 #1
0
        protected UserCustomAction GetCurrentCustomUserAction(object modelHost,
                                                              UserCustomActionDefinition customActionModel)
        {
            UserCustomActionCollection userCustomActions = null;

            return(GetCurrentCustomUserAction(modelHost, customActionModel, out userCustomActions));
        }
コード例 #2
0
        private UserCustomAction GetCurrentCustomUserAction(object modelHost, UserCustomActionDefinition customActionModel
                                                            , out UserCustomActionCollection userCustomActions)
        {
            if (modelHost is SiteModelHost)
            {
                userCustomActions = (modelHost as SiteModelHost).HostSite.UserCustomActions;
            }
            else if (modelHost is WebModelHost)
            {
                userCustomActions = (modelHost as WebModelHost).HostWeb.UserCustomActions;
            }
            else if (modelHost is ListModelHost)
            {
                userCustomActions = (modelHost as ListModelHost).HostList.UserCustomActions;
            }
            else
            {
                throw new Exception(string.Format("modelHost of type {0} is not supported.", modelHost.GetType()));
            }

            var context = userCustomActions.Context;

            context.Load(userCustomActions);
            context.ExecuteQueryWithTrace();

            return(userCustomActions.FirstOrDefault(a => !string.IsNullOrEmpty(a.Name) && a.Name.ToUpper() == customActionModel.Name.ToUpper()));
        }
コード例 #3
0
        public override ModelNode ReverseSingleHost(object reverseHost, ReverseOptions options)
        {
            var item = (reverseHost as UserCustomActionReverseHost).HostUserCustomAction;

            var def = new UserCustomActionDefinition();

            def.Title       = item.Title;
            def.Name        = item.Name;
            def.Description = item.Description;

            def.Group = item.Group;

            def.ScriptSrc   = item.ScriptSrc;
            def.ScriptBlock = item.ScriptBlock;

            def.Location = item.Location;
            def.Sequence = item.Sequence;

            def.Url = item.Url;

            def.RegistrationId   = item.RegistrationId;
            def.RegistrationType = item.RegistrationType.ToString();

            return(new UserCustomActionModelNode
            {
                Options = { RequireSelfProcessing = true },
                Value = def
            });
        }
コード例 #4
0
        protected virtual void ProcessLocalization(SPUserCustomAction obj, UserCustomActionDefinition definition)
        {
            if (definition.TitleResource.Any())
            {
                foreach (var locValue in definition.TitleResource)
                {
                    LocalizationService.ProcessUserResource(obj, obj.TitleResource, locValue);
                }
            }

            if (definition.DescriptionResource.Any())
            {
                foreach (var locValue in definition.DescriptionResource)
                {
                    LocalizationService.ProcessUserResource(obj, obj.DescriptionResource, locValue);
                }
            }

            if (definition.CommandUIExtensionResource.Any())
            {
                foreach (var locValue in definition.CommandUIExtensionResource)
                {
                    LocalizationService.ProcessUserResource(obj, obj.CommandUIExtensionResource, locValue);
                }
            }
        }
コード例 #5
0
        //[SampleMetadataTag(Name = BuiltInTagNames.SampleHidden)]
        public void CanDeployUserCustomActionOnWeb()
        {
            var webLogger = new UserCustomActionDefinition
            {
                Name        = "m2WebLogger",
                Location    = "ScriptLink",
                ScriptBlock = "console.log('site logger on web:' + _spPageContextInfo.webAbsoluteUrl);",
                Sequence    = 1800
            };

            var loggerWeb = new WebDefinition
            {
                Title       = "M2 Logger Web",
                Url         = "m2logging",
                WebTemplate = BuiltInWebTemplates.Collaboration.TeamSite
            };

            var model = SPMeta2Model.NewWebModel(web =>
            {
                web.AddWeb(loggerWeb, subWeb =>
                {
                    subWeb.AddUserCustomAction(webLogger);
                });
            });

            DeployModel(model);
        }
コード例 #6
0
        //[SampleMetadataTag(Name = BuiltInTagNames.SampleHidden)]
        public void CanDeployUserCustomActionWithJquery()
        {
            var jQueryFile = new ModuleFileDefinition
            {
                FileName  = "jquery-1.11.2.min.js",
                Content   = ModuleFileUtils.FromResource(GetType().Assembly, "SPMeta2.Docs.Modules.jquery-1.11.2.min.js"),
                Overwrite = true
            };

            var appScriptsFolder = new FolderDefinition
            {
                Name = "M2 App Scripts"
            };

            var jQueryCustomAction = new UserCustomActionDefinition
            {
                Name      = "m2jQuery",
                Location  = "ScriptLink",
                ScriptSrc = UrlUtility.CombineUrl(new string[]
                {
                    "~sitecollection",
                    BuiltInListDefinitions.StyleLibrary.GetListUrl(),
                    appScriptsFolder.Name,
                    jQueryFile.FileName
                }),
                Sequence = 1500
            };

            var jQuerySiteLogger = new UserCustomActionDefinition
            {
                Name        = "m2jQuerySiteLogger",
                Location    = "ScriptLink",
                ScriptBlock = "jQuery(document).ready( function() { console.log('jQuery site logger on site:' + _spPageContextInfo.siteAbsoluteUrl); } );",
                Sequence    = 1600
            };

            var siteModel = SPMeta2Model.NewSiteModel(site =>
            {
                site
                .AddUserCustomAction(jQueryCustomAction)
                .AddUserCustomAction(jQuerySiteLogger);
            });

            var webModel = SPMeta2Model.NewWebModel(web =>
            {
                web.AddHostList(BuiltInListDefinitions.StyleLibrary, list =>
                {
                    list.AddFolder(appScriptsFolder, folder =>
                    {
                        folder.AddModuleFile(jQueryFile);
                    });
                });
            });

            DeployModel(siteModel);
            DeployModel(webModel);
        }
コード例 #7
0
 protected virtual void ProcessLocalization(UserCustomAction obj, UserCustomActionDefinition definition)
 {
     ProcessGenericLocalization(obj, new Dictionary <string, List <ValueForUICulture> >
     {
         { "TitleResource", definition.TitleResource },
         { "DescriptionResource", definition.DescriptionResource },
         { "CommandUIExtensionResource", definition.CommandUIExtensionResource },
     });
 }
コード例 #8
0
        public void Deploy_CustomUserActions()
        {
            // Step 1, define security groups
            var signinAsDifferentUser = new UserCustomActionDefinition
            {
                Title    = "Signin as different user",
                Name     = "Signin as different user",
                Group    = BuiltInCustomActionLocationId.Microsoft.SharePoint.StandardMenu.Groups.SiteActions,
                Location = BuiltInCustomActionLocationId.Microsoft.SharePoint.StandardMenu.Location,
                Sequence = 2000,
                Url      = "~site/_layouts/closeConnection.aspx?loginasanotheruser=true"
            };

            // watch out 'customer site sync handler' log message while checking Chrome/IE with F12
            var customerSiteSyncSettings = new UserCustomActionDefinition
            {
                Title       = "Customer site sync handler",
                Name        = "Customer site sync handler",
                Location    = "ScriptLink",
                Sequence    = 2010,
                ScriptBlock = "console.log('customer site sync handler');"
            };

            var jQueryFromCDN = new UserCustomActionDefinition
            {
                Title     = "jquery",
                Name      = "jquery",
                Location  = "ScriptLink",
                Sequence  = 3000,
                ScriptSrc = "~site/Style Library/libs/jquery/1.11.1/jquery.min.js"
            };

            var jQueryFromCDNInitHandler = new UserCustomActionDefinition
            {
                Title       = "jquery-init-handler",
                Name        = "jquery-init-handler",
                Location    = "ScriptLink",
                Sequence    = 3010,
                ScriptBlock = "jQuery(document).ready( function() {  console.log('hello from jQuery'); });"
            };


            // Step 2, define web model and artifact relationships - add security groups t the web
            var model = SPMeta2Model
                        .NewSiteModel(site =>
            {
                site
                .AddUserCustomAction(signinAsDifferentUser)
                .AddUserCustomAction(customerSiteSyncSettings)
                .AddUserCustomAction(jQueryFromCDN)
                .AddUserCustomAction(jQueryFromCDNInitHandler);
            });

            // Step 3, deploy model
            DeploySiteModel(model);
        }
コード例 #9
0
 private void MapCustomAction(SPUserCustomAction existringAction, UserCustomActionDefinition customAction)
 {
     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;
 }
コード例 #10
0
        protected UserCustomAction GetCustomAction(SiteModelHost modelHost, UserCustomActionDefinition model)
        {
            var site    = modelHost.HostSite;
            var context = site.Context;

            context.Load(site, s => s.UserCustomActions);
            context.ExecuteQuery();

            return(site.UserCustomActions.FirstOrDefault(a => a.Name == model.Name));
        }
コード例 #11
0
        private void MapCustomAction(SPUserCustomAction existringAction, UserCustomActionDefinition customAction)
        {
            TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Updating user custom action properties.");

            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;

            existringAction.Sequence = customAction.Sequence;

            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 (existringAction.RegistrationType != SPUserCustomActionRegistrationType.List)
                {
                    existringAction.RegistrationType =
                        (SPUserCustomActionRegistrationType)
                        Enum.Parse(typeof(SPUserCustomActionRegistrationType), customAction.RegistrationType, true);
                }
            }

            var permissions = SPBasePermissions.EmptyMask;

            if (customAction.Rights != null && customAction.Rights.Count > 0)
            {
                foreach (var permissionString in customAction.Rights)
                {
                    permissions = permissions | (SPBasePermissions)Enum.Parse(typeof(SPBasePermissions), permissionString);
                }
            }

            existringAction.Rights = permissions;

            ProcessLocalization(existringAction, customAction);
        }
コード例 #12
0
        private void DeploySiteCustomAction(object modelHost, UserCustomActionDefinition model)
        {
            UserCustomActionCollection userCustomActions = null;
            var existingAction = GetCurrentCustomUserAction(modelHost, model, out userCustomActions);

            var context = userCustomActions.Context;

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model            = null,
                EventType        = ModelEventType.OnProvisioning,
                Object           = null,
                ObjectType       = typeof(UserCustomAction),
                ObjectDefinition = model,
                ModelHost        = modelHost
            });

            if (existingAction == null)
            {
                TraceService.Information((int)LogEventId.ModelProvisionProcessingNewObject, "Processing new user custom action");
                existingAction = userCustomActions.Add();
            }
            else
            {
                TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "Processing existing user custom action");
            }

            MapCustomAction(existingAction, model);

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model            = null,
                EventType        = ModelEventType.OnProvisioned,
                Object           = existingAction,
                ObjectType       = typeof(UserCustomAction),
                ObjectDefinition = model,
                ModelHost        = modelHost
            });


            TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Calling existingAction.Update()");
            existingAction.Update();

            context.ExecuteQueryWithTrace();
        }
コード例 #13
0
        //[SampleMetadataTag(Name = BuiltInTagNames.SampleHidden)]
        public void CanDeployUserCustomActionUnderSite()
        {
            var siteLogger = new UserCustomActionDefinition
            {
                Name        = "m2SiteLogger",
                Location    = "ScriptLink",
                ScriptBlock = "console.log('site logger on site:' + _spPageContextInfo.siteAbsoluteUrl);",
                Sequence    = 1000
            };

            var model = SPMeta2Model.NewSiteModel(site =>
            {
                site.AddUserCustomAction(siteLogger);
            });

            DeployModel(model);
        }
コード例 #14
0
        private void DeploySiteCustomAction(
            object modelHost,
            UserCustomActionDefinition customActionModel)
        {
            SPUserCustomActionCollection userCustomActions = null;
            var existingAction = GetCurrentCustomUserAction(modelHost, customActionModel, out userCustomActions);

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model            = null,
                EventType        = ModelEventType.OnProvisioning,
                Object           = existingAction,
                ObjectType       = typeof(SPUserCustomAction),
                ObjectDefinition = customActionModel,
                ModelHost        = modelHost
            });

            if (existingAction == null)
            {
                TraceService.Information((int)LogEventId.ModelProvisionProcessingNewObject, "Processing new user custom action");
                existingAction = userCustomActions.Add();
            }
            else
            {
                TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "Processing existing user custom action");
            }

            MapCustomAction(existingAction, customActionModel);

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model            = null,
                EventType        = ModelEventType.OnProvisioned,
                Object           = existingAction,
                ObjectType       = typeof(SPUserCustomAction),
                ObjectDefinition = customActionModel,
                ModelHost        = modelHost
            });

            existingAction.Update();
        }
コード例 #15
0
        private void ValidateModel(SiteModelHost siteModelHost, UserCustomActionDefinition customActionModel)
        {
            var customAction = GetCustomAction(siteModelHost, customActionModel);

            TraceUtils.WithScope(traceScope =>
            {
                var pair = new ComparePair <UserCustomActionDefinition, UserCustomAction>(customActionModel, customAction);

                traceScope.WriteLine(string.Format("Validating model:[{0}] custom action:[{1}]", customActionModel, customAction));

                traceScope.WithTraceIndent(trace => pair
                                           .ShouldBeEqual(trace, m => m.Title, o => o.Title)
                                           .ShouldBeEqual(trace, m => m.Description, o => o.Description)
                                           .ShouldBeEqual(trace, m => m.Group, o => o.Group)
                                           .ShouldBeEqual(trace, m => m.Name, o => o.Name)
                                           .ShouldBeEqual(trace, m => m.Sequence, o => o.Sequence)
                                           .ShouldBeEqual(trace, m => m.Location, o => o.Location));
            });
        }
コード例 #16
0
        private void DeploySiteCustomAction(SiteModelHost modelHost, UserCustomActionDefinition model)
        {
            var site    = modelHost.HostSite;
            var context = site.Context;

            var existingAction = GetCustomAction(modelHost, model);

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model            = null,
                EventType        = ModelEventType.OnProvisioning,
                Object           = null,
                ObjectType       = typeof(UserCustomAction),
                ObjectDefinition = model,
                ModelHost        = modelHost
            });

            if (existingAction == null)
            {
                existingAction = site.UserCustomActions.Add();
            }

            MapCustomAction(existingAction, model);

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model            = null,
                EventType        = ModelEventType.OnProvisioned,
                Object           = existingAction,
                ObjectType       = typeof(UserCustomAction),
                ObjectDefinition = model,
                ModelHost        = modelHost
            });

            existingAction.Update();

            context.ExecuteQuery();
        }
コード例 #17
0
        private void DeploySiteCustomAction(
            object modelHost,
            SPSite site, UserCustomActionDefinition customActionModel)
        {
            var existingAction = site.UserCustomActions.FirstOrDefault(a => a.Name == customActionModel.Name);

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model            = null,
                EventType        = ModelEventType.OnProvisioning,
                Object           = existingAction,
                ObjectType       = typeof(SPUserCustomAction),
                ObjectDefinition = customActionModel,
                ModelHost        = modelHost
            });

            if (existingAction == null)
            {
                existingAction = site.UserCustomActions.Add();
            }

            MapCustomAction(existingAction, customActionModel);

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model            = null,
                EventType        = ModelEventType.OnProvisioned,
                Object           = existingAction,
                ObjectType       = typeof(SPUserCustomAction),
                ObjectDefinition = customActionModel,
                ModelHost        = modelHost
            });

            existingAction.Update();
        }
コード例 #18
0
 public static TModelNode AddUserCustomAction <TModelNode>(this TModelNode model, UserCustomActionDefinition definition,
                                                           Action <UserCustomActionModelNode> action)
     where TModelNode : ModelNode, IUserCustomActionHostModelNode, new()
 {
     return(model.AddTypedDefinitionNode(definition, action));
 }
コード例 #19
0
 public static TModelNode AddUserCustomAction <TModelNode>(this TModelNode model, UserCustomActionDefinition definition)
     where TModelNode : ModelNode, IUserCustomActionHostModelNode, new()
 {
     return(AddUserCustomAction(model, definition, null));
 }
コード例 #20
0
 public static ModelNode AddUserCustomAction(this ModelNode model, UserCustomActionDefinition definition)
 {
     return(AddUserCustomAction(model, definition, null));
 }
コード例 #21
0
        private void MapCustomAction(SPUserCustomAction existringAction, UserCustomActionDefinition customAction)
        {
            TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Updating user custom action properties.");

            existringAction.Group       = customAction.Group;
            existringAction.Location    = customAction.Location;
            existringAction.Name        = customAction.Name;
            existringAction.ScriptBlock = customAction.ScriptBlock;
            existringAction.ScriptSrc   = customAction.ScriptSrc;

            // fallback for old models
            // fill out Title/Description with Name if NULLs
            // that needs for SP2016 to work well with translation exports

            // UserCustomAction without Title/Description breaks Translation Export #937
            // https://github.com/SubPointSolutions/spmeta2/issues/937

            if (!string.IsNullOrEmpty(customAction.Title))
            {
                existringAction.Title = customAction.Title;
            }
            else
            {
                existringAction.Title = customAction.Name;
            }

            if (!string.IsNullOrEmpty(customAction.Description))
            {
                existringAction.Description = customAction.Description;
            }
            else
            {
                existringAction.Description = customAction.Name;
            }

            existringAction.Url = customAction.Url;

            existringAction.Sequence = customAction.Sequence;

            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 (existringAction.RegistrationType != SPUserCustomActionRegistrationType.List)
                {
                    existringAction.RegistrationType =
                        (SPUserCustomActionRegistrationType)
                        Enum.Parse(typeof(SPUserCustomActionRegistrationType), customAction.RegistrationType, true);
                }
            }

            var permissions = SPBasePermissions.EmptyMask;

            if (customAction.Rights != null && customAction.Rights.Count > 0)
            {
                foreach (var permissionString in customAction.Rights)
                {
                    permissions = permissions | (SPBasePermissions)Enum.Parse(typeof(SPBasePermissions), permissionString);
                }
            }

            existringAction.Rights = permissions;

            ProcessLocalization(existringAction, customAction);
        }
コード例 #22
0
 public static ModelNode AddUserCustomAction(this ModelNode model, UserCustomActionDefinition definition, Action <ModelNode> action)
 {
     return(model.AddDefinitionNode(definition, action));
 }