private void CheckForUserCustomActions(ListCreator listCreator, List list, ClientContext ctx,
                                               AppManifestBase appManifest)
        {
            var builder = new CustomActionCreatorBuilder();

            appManifest.ListCreators = new Dictionary <string, ListCreator> {
                [listCreator.Title] = new ListCreator()
            };

            builder.GetCustomActionCreators(ctx, list, listCreator.Title, appManifest);
            listCreator.CustomActionCreators = appManifest.ListCreators[listCreator.Title].CustomActionCreators;
        }
Ejemplo n.º 2
0
        public void GetWebCreatorBuilder(SiteDefinition siteDefinition, WebCreator webDefinition,
                                         WebCreatorBuilderOptions options, ClientContext sourceContext, ClientContext baseContext)
        {
            _baseContext   = baseContext;
            _sourceContext = sourceContext;
            var js = new JavaScriptSerializer();
            var tempManifestFromBase = new AppManifestBase();

            if (webDefinition == null)
            {
                throw new ArgumentException("WebDefinition is required!");
            }
            if (options != null)
            {
                _options = options;
            }
            else
            {
                _options = new WebCreatorBuilderOptions();
            }

            var web = _sourceContext.Web;

            _sourceContext.Site.EnsureProperties(p => p.RootWeb);

            var isRootWeb = web.Url == _sourceContext.Site.RootWeb.Url;

            if (isRootWeb)
            {
                webDefinition.Url = "/";
            }
            //If this isn't the root and there isn't a value already,
            //set it to the site collection relative url by striping the rootweb url.
            else if (string.IsNullOrEmpty(webDefinition.Url))
            {
                webDefinition.Url = web.ServerRelativeUrl.Substring(_sourceContext.Site.RootWeb.ServerRelativeUrl.Length);
            }

            //Ensure that the Url is valid per the structure of the site definition
            if (!ValidateUrl(siteDefinition, webDefinition))
            {
                throw new InvalidOperationException(
                          "Web definition is invalid because the url either doesn't match the collection key or because it doesn't respect the hierarchy!");
            }

            if (siteDefinition.StorageType == StorageTypes.AzureStorage)
            {
                var storageInfo = siteDefinition.GetAzureStorageInfo();
                webDefinition.AppManifest.SetAzureStorageInfo(storageInfo.Account, storageInfo.AccountKey,
                                                              storageInfo.Container);
            }

            webDefinition.AppManifest.BaseFilePath =
                Path.GetFullPath(siteDefinition.BaseFilePath +
                                 (webDefinition.Url != "/" ? webDefinition.Url : string.Empty));

            GetBaseWebDefinition(webDefinition, isRootWeb);

            var creatorBuilder = new CreatorBuilder();

            creatorBuilder.VerboseNotify += (sender, args) => OnVerboseNotify(args.Message);
            if (_options.IncludeNavigation)
            {
                OnVerboseNotify("Getting top navigation");
                creatorBuilder.GetCreator(_sourceContext, web, "Top", webDefinition.AppManifest,
                                          CreatorTypes.Navigation);

                creatorBuilder.GetCreator(_baseContext, _baseContext.Web, "Top", tempManifestFromBase,
                                          CreatorTypes.Navigation);

                if (js.Serialize(webDefinition.AppManifest.Navigation.TopNavigationNodes) ==
                    js.Serialize(tempManifestFromBase.Navigation.TopNavigationNodes))
                {
                    OnVerboseNotify("No differences in top nav found.");
                    webDefinition.AppManifest.Navigation.TopNavigationNodes = null;
                }
                else
                {
                    OnVerboseNotify("Differences in top nav found.");
                }

                OnVerboseNotify("Getting left navigation");

                creatorBuilder.GetCreator(_sourceContext, web, "Left", webDefinition.AppManifest,
                                          CreatorTypes.Navigation);

                creatorBuilder.GetCreator(_baseContext, _baseContext.Web, "Left", tempManifestFromBase,
                                          CreatorTypes.Navigation);

                if (js.Serialize(webDefinition.AppManifest.Navigation.LeftNavigationNodes) ==
                    js.Serialize(tempManifestFromBase.Navigation.LeftNavigationNodes))
                {
                    OnVerboseNotify("No differences in left nav found.");
                    webDefinition.AppManifest.Navigation.LeftNavigationNodes = null;
                }
                else
                {
                    OnVerboseNotify("Differences in left nav found.");
                }
            }
            if (_options.IncludeRemoteEventReceivers)
            {
                OnVerboseNotify("Getting remote event receivers");
                creatorBuilder.GetCreator(_sourceContext, web, string.Empty, webDefinition.AppManifest,
                                          CreatorTypes.RemoteEvents);

                if (webDefinition.AppManifest.RemoteEventRegistrationCreators == null ||
                    webDefinition.AppManifest.RemoteEventRegistrationCreators.Count == 0)
                {
                    OnVerboseNotify("No remote event receivers found");
                }
                else
                {
                    creatorBuilder.GetCreator(_baseContext, _baseContext.Web, string.Empty, tempManifestFromBase,
                                              CreatorTypes.RemoteEvents);

                    var matchingReceivers =
                        webDefinition.AppManifest.RemoteEventRegistrationCreators.Where(
                            s =>
                            tempManifestFromBase.RemoteEventRegistrationCreators.FirstOrDefault(
                                b =>
                                b.ListTitle == s.ListTitle && b.EndpointUrl == s.EndpointUrl &&
                                b.EventReceiverType == s.EventReceiverType && b.Eventname == s.Eventname) !=
                            null).ToList();

                    if (matchingReceivers.Count != 0)
                    {
                        for (int c = matchingReceivers.Count; c >= 0; c--)
                        {
                            webDefinition.AppManifest.RemoteEventRegistrationCreators.Remove(matchingReceivers[c]);
                        }
                    }
                    if (webDefinition.AppManifest.RemoteEventRegistrationCreators.Count == 0)
                    {
                        OnVerboseNotify("No remote event receivers found");
                    }
                    else
                    {
                        OnVerboseNotify("Found remote event receivers");
                    }
                }
            }
            if (_options.IncludeLookAndFeel)
            {
                OnVerboseNotify("Getting look and feel");
                creatorBuilder.GetCreator(_sourceContext, web, string.Empty, webDefinition.AppManifest,
                                          CreatorTypes.LookAndFeel);
                creatorBuilder.GetCreator(_baseContext, _baseContext.Web, string.Empty, tempManifestFromBase,
                                          CreatorTypes.LookAndFeel);

                if (webDefinition.AppManifest.LookAndFeel.SiteLogoUrl == tempManifestFromBase.LookAndFeel.SiteLogoUrl)
                {
                    webDefinition.AppManifest.LookAndFeel.SiteLogoUrl = string.Empty;
                }
                else
                {
                    OnVerboseNotify($"Setting SiteLogoUrl to {webDefinition.AppManifest.LookAndFeel.SiteLogoUrl}");
                }
                if (webDefinition.AppManifest.LookAndFeel.AlternateCssUrl == tempManifestFromBase.LookAndFeel.AlternateCssUrl)
                {
                    webDefinition.AppManifest.LookAndFeel.AlternateCssUrl = string.Empty;
                }
                else
                {
                    OnVerboseNotify($"Setting AlternateCssUrl to {webDefinition.AppManifest.LookAndFeel.AlternateCssUrl}");
                }
                if (webDefinition.AppManifest.LookAndFeel.CustomMasterPageUrl == tempManifestFromBase.LookAndFeel.CustomMasterPageUrl)
                {
                    webDefinition.AppManifest.LookAndFeel.CustomMasterPageUrl = string.Empty;
                }
                else
                {
                    OnVerboseNotify($"Setting CustomMasterPageUrl to {webDefinition.AppManifest.LookAndFeel.CustomMasterPageUrl}");
                }
                if (webDefinition.AppManifest.LookAndFeel.DefaultMasterPageUrl == tempManifestFromBase.LookAndFeel.DefaultMasterPageUrl)
                {
                    webDefinition.AppManifest.LookAndFeel.DefaultMasterPageUrl = string.Empty;
                }
                else
                {
                    OnVerboseNotify($"Setting DefaultMasterPageUrl to {webDefinition.AppManifest.LookAndFeel.DefaultMasterPageUrl}");
                }

                if (js.Serialize(webDefinition.AppManifest.LookAndFeel.CurrentComposedLook) ==
                    js.Serialize(tempManifestFromBase.LookAndFeel.CurrentComposedLook))
                {
                    webDefinition.AppManifest.LookAndFeel.CurrentComposedLook = null;
                }
                else
                {
                    OnVerboseNotify("Found differences in current composed looks.");
                }
            }
            var customActionCreatorBuilder = new CustomActionCreatorBuilder();

            customActionCreatorBuilder.VerboseNotify += (sender, args) => OnVerboseNotify(args.Message);
            if (_options.IncludeSiteCustomActions || _options.IncludeWebCustomActions)
            {
                if (_options.IncludeSiteCustomActions)
                {
                    OnVerboseNotify("Getting site custom actions");
                    customActionCreatorBuilder.GetCustomActionCreators(_sourceContext, web, webDefinition.AppManifest,
                                                                       true);
                    customActionCreatorBuilder.GetCustomActionCreators(_baseContext, _baseContext.Web,
                                                                       tempManifestFromBase, true);
                }
                if (_options.IncludeWebCustomActions)
                {
                    OnVerboseNotify("Getting web custom actions");
                    customActionCreatorBuilder.GetCustomActionCreators(_sourceContext, web, webDefinition.AppManifest,
                                                                       false);
                    customActionCreatorBuilder.GetCustomActionCreators(_baseContext, _baseContext.Web,
                                                                       tempManifestFromBase, false);
                }
                var matchingCustomActions =
                    webDefinition.AppManifest.CustomActionCreators.Values.Where(
                        sca =>
                        tempManifestFromBase.CustomActionCreators.Values.FirstOrDefault(
                            bca => bca.SiteScope == sca.SiteScope && bca.Url == sca.Url &&
                            bca.CommandUIExtension == sca.CommandUIExtension &&
                            bca.Description == sca.Description &&
                            bca.Group == sca.Group && bca.ImageUrl == sca.ImageUrl &&
                            bca.Location == sca.Location &&
                            bca.RegistrationId == sca.RegistrationId &&
                            bca.ScriptBlock == sca.ScriptBlock &&
                            bca.ScriptSrc == sca.ScriptSrc && bca.Title == sca.Title) !=
                        null);

                foreach (var matchingCustomAction in matchingCustomActions)
                {
                    webDefinition.AppManifest.CustomActionCreators.Remove(matchingCustomAction.Title);
                }
                if (webDefinition.AppManifest.CustomActionCreators.Count > 0)
                {
                    OnVerboseNotify("Found custom actions.");
                }
            }
            if (_options.IncludeFilesFromTheseLibraries.Count > 0)
            {
                var fileCreatorBuilder = new FileCreatorBuilder();
                fileCreatorBuilder.VerboseNotify += (sender, args) => OnVerboseNotify(args.Message);
                foreach (var libraryTitle in _options.IncludeFilesFromTheseLibraries)
                {
                    var       library = web.Lists.GetByTitle(libraryTitle);
                    CamlQuery query   = CamlQuery.CreateAllItemsQuery(2000);

                    var fileItems = library.GetItems(query);
                    _sourceContext.Load(fileItems, fi => fi.Include(f => f.FieldValuesAsText, f => f.File));
                    _sourceContext.ExecuteQuery();

                    foreach (var fileItem in fileItems)
                    {
                        //Folders items don't have an associated file
                        if (fileItem.File.ServerObjectIsNull != null && fileItem.File.ServerObjectIsNull == false)
                        {
                            fileCreatorBuilder.GetFileCreator(_sourceContext, web,
                                                              fileItem.File.ServerRelativeUrl.Replace(web.ServerRelativeUrl, ""),
                                                              webDefinition.AppManifest.BaseFilePath, webDefinition.AppManifest, false);
                        }
                    }
                }
            }
            if (_options.IncludeFilesFromTheseFolders.Count > 0)
            {
                var baseUrl            = web.ServerRelativeUrl == "/" ? string.Empty : web.ServerRelativeUrl;
                var fileCreatorBuilder = new FileCreatorBuilder();
                fileCreatorBuilder.VerboseNotify += (sender, args) => OnVerboseNotify(args.Message);
                foreach (var folder in _options.IncludeFilesFromTheseFolders)
                {
                    var searchFolder = folder;
                    if (!searchFolder.StartsWith("/"))
                    {
                        searchFolder = "/" + folder;
                    }
                    var containerFolderUrl = baseUrl + searchFolder;
                    var spFolder           = web.GetFolderByServerRelativeUrl(containerFolderUrl);
                    _sourceContext.Load(spFolder, f => f.Files.Include(file => file.Name, file => file.ServerRelativeUrl));
                    _sourceContext.ExecuteQueryRetry();
                    foreach (var file in spFolder.Files)
                    {
                        fileCreatorBuilder.GetFileCreator(_sourceContext, web,
                                                          file.ServerRelativeUrl.Replace(web.ServerRelativeUrl, ""),
                                                          webDefinition.AppManifest.BaseFilePath, webDefinition.AppManifest, false);
                    }
                }
            }
        }