public SPOUserCustomAction Add()
        {
            var uca = new SPOUserCustomAction(_userCustomActionCollection.Add());

            AddChild(uca);
            return(uca);
        }
Пример #2
0
        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();
            }
        }
Пример #3
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();
        }
Пример #4
0
        private static bool AddCustomActionImplementation(ClientObject clientObject, CustomActionEntity customAction)
        {
            UserCustomAction           targetAction    = null;
            UserCustomActionCollection existingActions = null;

            if (clientObject is Web)
            {
                var web = (Web)clientObject;

                existingActions = web.UserCustomActions;
                web.Context.Load(existingActions);
                web.Context.ExecuteQueryRetry();

                targetAction = web.UserCustomActions.FirstOrDefault(uca => uca.Name == customAction.Name);
            }
            else
            {
                var site = (Site)clientObject;

                existingActions = site.UserCustomActions;
                site.Context.Load(existingActions);
                site.Context.ExecuteQueryRetry();

                targetAction = site.UserCustomActions.FirstOrDefault(uca => uca.Name == customAction.Name);
            }

            if (targetAction == null)
            {
                // If we're removing the custom action then we need to leave when not found...else we're creating the custom action
                if (customAction.Remove)
                {
                    return(true);
                }
                else
                {
                    targetAction = existingActions.Add();
                }
            }
            else if (customAction.Remove)
            {
                targetAction.DeleteObject();
                clientObject.Context.ExecuteQueryRetry();
                return(true);
            }

            targetAction.Name        = customAction.Name;
            targetAction.Description = customAction.Description;
            targetAction.Location    = customAction.Location;

            if (customAction.Location == JavaScriptExtensions.SCRIPT_LOCATION)
            {
                targetAction.ScriptBlock = customAction.ScriptBlock;
                targetAction.ScriptSrc   = customAction.ScriptSrc;
            }
            else
            {
                targetAction.Sequence = customAction.Sequence;
                targetAction.Url      = customAction.Url;
                targetAction.Group    = customAction.Group;
                targetAction.Title    = customAction.Title;
                targetAction.ImageUrl = customAction.ImageUrl;

                if (customAction.RegistrationId != null)
                {
                    targetAction.RegistrationId = customAction.RegistrationId;
                }

                if (customAction.CommandUIExtension != null)
                {
                    targetAction.CommandUIExtension = customAction.CommandUIExtension;
                }

                if (customAction.Rights != null)
                {
                    targetAction.Rights = customAction.Rights;
                }

                if (customAction.RegistrationType.HasValue)
                {
                    targetAction.RegistrationType = customAction.RegistrationType.Value;
                }
            }

            targetAction.Update();
            if (clientObject is Web)
            {
                var web = (Web)clientObject;
                web.Context.Load(web, w => w.UserCustomActions);
                web.Context.ExecuteQueryRetry();
            }
            else
            {
                var site = (Site)clientObject;
                site.Context.Load(site, s => s.UserCustomActions);
                site.Context.ExecuteQueryRetry();
            }

            return(true);
        }