понедельник, 25 июня 2012 г.

Развертывание master pages с помощью vs

1. Для того, чтобы развернуть master pages создадим пустой проект sp и развернем на http://localhost/.
2. Добавим к нему новый элемент - module, назовем его masterpage.
3. Изменяем Elements.xml:
c
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="masterpage">
    <File Path="masterpage\Sample.txt" Url="masterpage/Sample.txt" />
  </Module>
</Elements>

на
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="masterpage" List="116" Url="_catalogs/masterpage">
    <File Path="masterpage\new_v4.master" Url="new_v4.master" Type="GhostableInLibrary" IgnoreIfAlreadyExists="TRUE" >     
    </File>
  </Module>
</Elements>

4. Sample.txt переименовываем на имя нашей страницы - new_v4.master
5. В  new_v4.master вставляем код нашей страницы.
6. Добавим EventReceiver к feature проекта и scope поставим Site.
7. Добавим код:
//При активации
public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite siteCollection = properties.Feature.Parent as SPSite;
            if (siteCollection != null)
            {
                SPWeb topLevelSite = siteCollection.RootWeb;

                string WebAppRelativePath = topLevelSite.ServerRelativeUrl;
                if (!WebAppRelativePath.EndsWith("/"))
                {
                    WebAppRelativePath += "/";
                }

                foreach (SPWeb site in siteCollection.AllWebs)
                {
                    site.MasterUrl = WebAppRelativePath +
                                     "_catalogs/masterpage/new_v4.master";
                    site.CustomMasterUrl = WebAppRelativePath +
                                           "_catalogs/masterpage/new_v4.master";

                    site.UIVersion = 4;
                    site.Update();
                }
            }
        }

8. Для того чтобы, при деактивации  вернулась стандартная страница в метод деактивации добавим:

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSite siteCollection = properties.Feature.Parent as SPSite;
            if (siteCollection != null)
            {
                SPWeb topLevelSite = siteCollection.RootWeb;

                // Calculate relative path to site from Web Application root.
                string WebAppRelativePath = topLevelSite.ServerRelativeUrl;
                if (!WebAppRelativePath.EndsWith("/"))
                {
                    WebAppRelativePath += "/";
                }

                // Enumerate through each site and apply branding.
                foreach (SPWeb site in siteCollection.AllWebs)
                {
                    site.MasterUrl = WebAppRelativePath +
                                     "_catalogs/masterpage/v4.master";
                    site.CustomMasterUrl = WebAppRelativePath +
                                           "_catalogs/masterpage/v4.master";

                    site.UIVersion = 4;
                    site.Update();
                }
            }
        }

Развертывание мастер страниц для профиля пользователя, такая же. Отличия лишь в том что, разворачивать будем на http://localhost/my/ .
Elements.xml:
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="newMysite" List="116" Url="_catalogs/masterpage">
    <File Path="newMysite\new_mysite.master" Url="new_mysite.master" Type="GhostableInLibrary" IgnoreIfAlreadyExists="TRUE">
    </File>
  </Module>
</Elements>

Код обработчика:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            this.SetMasterPage((SPSite)properties.Feature.Parent, "_catalogs/masterpage/new_mysite.master");
        }

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            this.SetMasterPage((SPSite)properties.Feature.Parent, "_catalogs/masterpage/mysite.master");
        }

        private void SetMasterPage(SPSite site, String masterPageRelativeUrl)
        {
            string WebAppRelativePath = site.RootWeb.ServerRelativeUrl;

            if (!WebAppRelativePath.EndsWith("/")) WebAppRelativePath += "/";

            string url = WebAppRelativePath + masterPageRelativeUrl;

            foreach (SPWeb web in site.AllWebs)
            {
                web.MasterUrl = url;
                web.CustomMasterUrl = url;
                web.UIVersion = 4;
                web.Update();
            }
        }

Вот собственно и все!

четверг, 21 июня 2012 г.

Скрыть риббон от пользователей но оставить его администраторам

Для того чтобы скрыть риббон от пользователей нужно немного поправить master page.

1.Открываем мастер страниц в sp designer и ищем div с id = s4-ribbonrow:

2. Добавим к этому диву style="display:none" , теперь риббон  скрыт ото всех.
3. Ну и для того чтобы  администраторы могли его видеть, в конце дива добавим:
<Sharepoint:SPSecurityTrimmedControl ID="SPSecurityTrimmedControl1" runat="server" PermissionsString="ManageWeb">
    <script type="text/javascript">
        document.getElementById("s4-ribbonrow").style.display = "block";
    </script>
</Sharepoint:SPSecurityTrimmedControl>
4. Сохраняем и опубликовываем  страницу.