示例#1
0
        public static List EnsureLocalNavConfigList(Web web)
        {
            List list = null;

            try
            {
                Logger.LogInfoMessage(String.Format("Ensuring Local Nav Config list for {0} ...", web.Url), true);

                list = web.GetListByUrl(Constants.LocalNavListWebRelativeUrl);
                if (list == null)
                {
                    Logger.LogInfoMessage(String.Format("Creating Local Nav Config list ..."), true);
                    list = web.CreateList(Microsoft.SharePoint.Client.ListTemplateType.GenericList, Constants.LocalNavListTitle, false, true, Constants.LocalNavListWebRelativeUrl, false);
                }

                FieldCollection     fields      = list.Fields;
                View                defaultView = list.DefaultView;
                ViewFieldCollection viewFields  = defaultView.ViewFields;
                web.Context.Load(fields);
                web.Context.Load(defaultView);
                web.Context.Load(viewFields);
                web.Context.ExecuteQueryRetry();

                Logger.LogInfoMessage(String.Format("- Ensuring List Field [{0}] ...", Constants.PnPDisplayOrder_DisplayName), false);
                if (list.FieldExistsById(new Guid(Constants.PnPDisplayOrder_GUID)) == false)
                {
                    fields.Add(web.GetFieldById <Field>(new Guid(Constants.PnPDisplayOrder_GUID), true));
                    viewFields.Add(Constants.PnPDisplayOrder_InternalName);
                    defaultView.Update();
                }
                Logger.LogInfoMessage(String.Format("- Ensuring List Field [{0}] ...", Constants.PnPLinkText_DisplayName), false);
                if (list.FieldExistsById(new Guid(Constants.PnPLinkText_GUID)) == false)
                {
                    fields.Add(web.GetFieldById <Field>(new Guid(Constants.PnPLinkText_GUID), true));
                    viewFields.Add(Constants.PnPLinkText_InternalName);
                    defaultView.Update();
                }
                Logger.LogInfoMessage(String.Format("- Ensuring List Field [{0}] ...", Constants.PnPLinkUrl_DisplayName), false);
                if (list.FieldExistsById(new Guid(Constants.PnPLinkUrl_GUID)) == false)
                {
                    fields.Add(web.GetFieldById <Field>(new Guid(Constants.PnPLinkUrl_GUID), true));
                    viewFields.Add(Constants.PnPLinkUrl_InternalName);
                    defaultView.Update();
                }
                string viewQuery = String.Format(Constants.ListViewQueryFormatString, Constants.PnPDisplayOrder_InternalName);
                if (!defaultView.ViewQuery.Equals(viewQuery, StringComparison.InvariantCultureIgnoreCase))
                {
                    defaultView.ViewQuery = viewQuery;
                    defaultView.Update();
                }
                list.Update();
                web.Context.ExecuteQueryRetry();

                //Initialize the list
                ListItemCollection listItems = list.GetItems(CamlQuery.CreateAllItemsQuery());
                web.Context.Load(listItems);
                web.Context.ExecuteQueryRetry();

                // Initialize the list only if it is empty.
                if (listItems.Count == 0)
                {
                    Logger.LogInfoMessage(String.Format("Initializing Local Nav Config list ..."), false);

                    for (int i = 0; i < 5; i++)
                    {
                        Logger.LogInfoMessage(String.Format("- Adding list item..."), false);

                        ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
                        ListItem listItem = list.AddItem(itemCreateInfo);

                        listItem["Title"] = String.Format("Local Link {0}", i + 1);
                        listItem[Constants.PnPDisplayOrder_InternalName] = (i + 1);
                        listItem[Constants.PnPLinkText_InternalName]     = String.Format("{0} Link {1}", web.Title, i + 1);
                        listItem[Constants.PnPLinkUrl_InternalName]      = Constants.LocalNavLinkUrl;
                        listItem.Update();
                        web.Context.ExecuteQueryRetry();
                    }
                }
                Logger.LogSuccessMessage(String.Format("Ensured Local Nav Config list for {0}", web.Url), false);
            }
            catch (Exception ex)
            {
                Logger.LogErrorMessage(String.Format("EnsureLocalNavConfigList() failed for {0}: Error={1}", web.Url, ex.Message), false);
            }

            return(list);
        }
示例#2
0
        public static List EnsurePortalConfigList(Web web)
        {
            List list = null;

            try
            {
                Logger.LogInfoMessage(String.Format("Ensuring Portal Config list for {0} ...", web.Url), true);

                list = web.GetListByUrl(Constants.ConfigurationListWebRelativeUrl);
                if (list == null)
                {
                    Logger.LogInfoMessage(String.Format("Creating Portal Config list ..."), true);
                    list = web.CreateList(Microsoft.SharePoint.Client.ListTemplateType.GenericList, Constants.ConfigurationListTitle, false, true, Constants.ConfigurationListWebRelativeUrl, false);
                }

                FieldCollection     fields      = list.Fields;
                View                defaultView = list.DefaultView;
                ViewFieldCollection viewFields  = defaultView.ViewFields;
                web.Context.Load(fields);
                web.Context.Load(defaultView);
                web.Context.Load(viewFields);
                web.Context.ExecuteQueryRetry();

                Logger.LogInfoMessage(String.Format("- Ensuring List Field [{0}] ...", Constants.PnPConfigKey_DisplayName), false);
                if (list.FieldExistsById(new Guid(Constants.PnPConfigKey_GUID)) == false)
                {
                    fields.Add(web.GetFieldById <Field>(new Guid(Constants.PnPConfigKey_GUID), true));
                    viewFields.Add(Constants.PnPConfigKey_InternalName);
                    defaultView.Update();
                }
                Logger.LogInfoMessage(String.Format("- Ensuring List Field [{0}] ...", Constants.PnPConfigValue_DisplayName), false);
                if (list.FieldExistsById(new Guid(Constants.PnPConfigValue_GUID)) == false)
                {
                    fields.Add(web.GetFieldById <Field>(new Guid(Constants.PnPConfigValue_GUID), true));
                    viewFields.Add(Constants.PnPConfigValue_InternalName);
                    defaultView.Update();
                }
                string viewQuery = String.Format(Constants.ListViewQueryFormatString, Constants.PnPConfigKey_InternalName);
                if (!defaultView.ViewQuery.Equals(viewQuery, StringComparison.InvariantCultureIgnoreCase))
                {
                    defaultView.ViewQuery = viewQuery;
                    defaultView.Update();
                }
                list.Update();
                web.Context.ExecuteQueryRetry();

                //Initialize the list
                ListItemCollection listItems = list.GetItems(CamlQuery.CreateAllItemsQuery());
                web.Context.Load(listItems);
                web.Context.ExecuteQueryRetry();

                // Initialize the list only if it is empty.
                if (listItems.Count == 0)
                {
                    Logger.LogInfoMessage(String.Format("Initializing Portal Config list ..."), false);

                    Logger.LogInfoMessage(String.Format("- Adding list item..."), false);

                    ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
                    ListItem listItem = list.AddItem(itemCreateInfo);

                    listItem["Title"] = Constants.ConfigurationListFooterKey;
                    listItem[Constants.PnPConfigKey_InternalName]   = Constants.ConfigurationListFooterKey;
                    listItem[Constants.PnPConfigValue_InternalName] = String.Format(
                        "<table border='0' cellpadding='0' cellspacing='0' align='center'>" +
                        "<tbody style='float:right;'>" +
                        "<tr>" +
                        "<td align='center' style=''>" +
                        "<a href='{0}' style='color: white'>Footer 1</a> | " +
                        "<a href='{0}' style='color: white'>Footer 2</a> | " +
                        "<a href='{0}' style='color: white'>Footer 3</a> | " +
                        "<a href='{0}' style='color: white'>Footer 4</a> | " +
                        "<a href='{0}' style='color: white'>Footer 5</a> " +
                        "</td>" +
                        "</tr>" +
                        "<tr>" +
                        "<td align='center' style=''>" +
                        "{1} Contoso, Inc. All Rights Reserved." +
                        "</td>" +
                        "</tr>" +
                        "</tbody>" +
                        "</table>", Constants.FooterNavLinkUrl, DateTime.Now.Year);

                    listItem.Update();
                    web.Context.ExecuteQueryRetry();
                }
                Logger.LogSuccessMessage(String.Format("Ensured Portal Config list for {0}", web.Url), false);
            }
            catch (Exception ex)
            {
                Logger.LogErrorMessage(String.Format("EnsurePortalConfigList() failed for {0}: Error={1}", web.Url, ex.Message), false);
            }

            return(list);
        }