in

didierdanse.net

Site personnel de Didier Danse
Didier Danse's Personnal Website
Microsoft Most Valuable Professional SharePoint

didierdanse.net - Development News

September 2009 - Posts

  • SharePoint: How to limit available site templates for site creation

    Once again, a colleague ask me if there is some possibilities to remove some site templates from the site creation page as she don’t want to allow power users to create sites based on custom site templates.

    The easiest way to do this is to click on Page Layouts and Site Templates on the Site Settings page. This link points at //_Layouts/AreaTemplateSettings.aspx">//_Layouts/AreaTemplateSettings.aspx">http://<yoursharepointurl>/<sitename>/_Layouts/AreaTemplateSettings.aspx page. This link is only available at the root site of the site collection but you can navigate to the page using the previous link.

    On this page, you will be able able to select site templates from available site templates list: 

    From a developer or an administrator perspective, there is also the possibility to use some code to manage available site templates.

    Here is a example of this code:

    public void SetAvailableTemplates(List<string> templateNames)
    {
         SetAvailableTemplates(templateNames, 0);
    }
    
    public void SetAvailableTemplates(List<string> templateNames, uint lcid)
    {
        using (SPSite site = new SPSite("http://<yoursharepointurl>/<sitename>"))
        {
            using (SPWeb web = site.OpenWeb())
            {
                if (lcid == 0) lcid = web.Language;
                Collection<SPWebTemplate> availablesTemplates = new Collection<SPWebTemplate>;
                foreach (SPWebTemplate template in site.GetWebTemplates(lcid))
                    if (templateNames.Keys.Contains(template.Name))
                        availablesTemplates.Add(template);
            
                if (availablesTemplates.Count > 0)
                {
                   // Code is different if site is a publishing site or not as some properties and methods are not exposed directly
                    if (PublishingWeb.IsPublishingWeb(web))
                    {
                        PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);
                        if (lcid == 0) publishingWeb.SetAvailableCrossLanguageWebTemplates(availablesTemplates, false);
                        else publishingWeb.SetAvailableWebTemplates(availablesTemplates, lcid, false);
                    }
                    else
                    {
                        if (lcid == 0) web.SetAvailableCrossLanguageWebTemplates(availablesTemplates);
                        else web.SetAvailableWebTemplates(availablesTemplates, lcid);
                        web.AllProperties["__InheritWebTemplates"] = false.ToString();
                        web.Update();
                    }
                }
            }
        }
    }

    Now you can package this as a STSADM extension, a feature or anything else that can be imagined to manipulate your SharePoint.



  • SharePoint: Farm, web application, sites collection, sites and subsites, how to know what to use?

    Because of the scalability of SharePoint, each one who has to organize a SharePoint infrastructure is quickly set against an important question: What to use and at what time? There isn’t only one answer to this. Each element that composes an complete Infrastrcutre has typical properties and objectives. So we have to use the best container according to objectives and content.

    There is some kind of containers. The most important are:

    • 1a) SharePoint Farm (SPFarm from a SharePoint point of view)
    • 1b) Shared Services Provider
    • 2) Web Application (SPWebApplication)
    • 3) Sites Collection (SPSite)
    • 4) Sites (SPWeb)
    • 4) Sub sites (SPWeb)

    Mainly the first four elements (in blue) are used for technical reasons while the two other ones are used to structure sites and data from a logical point of view.

    So have a quick look through elements that are used in a SharePoint infrastructure. For more details, please use the best tool in the developers’ world: Internet.

    1.A) SharePoint Farm

    A SharePoint farm is a group of front-end servers, applicative servers (Shared Services), indexing servers and database servers. A SharePoint farm can be deployed to a particular location according to objectives (Internet access, geographical localization, …).

    image

    1.B) Shared Services Provider

    SharePoint Services Provider (SSP) is the composition of some farm-level services. These services are shared between all web applications and thus also between sites collections.

    These services are responsible of:

    • User Profile (profile properties, synch parameters)
    • Search (scopes, properties mappings, content sources, …)
    • Excel Services
    • Business Data Catalog

    image

    It is possible to use more than one SSP but it is highly recommended not to use more than useful. Using a lot of SSP can result to a complex SharePoint administration.

    2) Web Application

    There is nothing new for ITs or developers that already worked with sites or applications powered by ASP.NET, PHP or all server-side technologies. A web application characteristics are:

    • A site in IIS
    • A particular url
    • A authentication method associated with this web application

    image

    We can notice that a web application uses its own database in a SharePoint context (to be more precise, it is the case for new application. There is the possibility to create Web Applications extensions. These extensions can use different parameters that the main web application. We will use this when we want to share data between web applications and use different authentication modes for example).

    Furthermore, SharePoint is an ASP.NET application so it uses Web.config files. We will be able to add differents entries according to the objectives of the web application. We will remove some features if the web application is used by partners for example.

    Finally, a web application is always associated with a SSP.

    3) Sites Collection

    A sites collection is a virtual container (there is no physical file as it is the case for previously described elements) and sites collection structure is stored in a configuration database.

    Form an IT point of view, sites collection has some advantages. The main advantages are:

    • Content quota templates
    • Ease of maintenance (backup / restore is performed by site collection by default)

    image

    From a content manager point of view, we will notice that an administrator is defined each site collection. This administrator can manage:

    • Second level Recycle bin
    • Customized search
    • Users of the site collection

    Lastly, site collections is used to bring customized elements together and shared them with a audience that will access data across this sites collection (per example a department):

    • Master Pages
    • Images
    • Sites and lists Templates
    • Web parts

    There is always a root site in a site collection.

    image

    4) Sites and subsites

    In the beginning of this post, we told a lot of time of IT guys, a little bit about developers but I didn’t told about about end users even though they are the driving force of SharePoint content.

    At this level, everything is related to content management. According to the users permissions, they can:

    • Create sites
    • Create lists
    • Attach workflow to lists
    • Grant permissions
    • Restore deleted content
    • A lot of others operations!

    At this level, there is only one important thing to manage and organize data: company best practices and rules. It doesn’t matter where data are logically based, it will be physically stored in the same database. However, data should be spread all around sites and sub-sites. Do not hesitate to use important feature as permissions inheritance. It will help to organize data efficiently.

    image

    It is possible to add some code or reporting tools but the most important thing is the end-user and power-user training.

     

    Here is a SharePoint structure example:

    • Farm
      • Web Application 1
        • Sites collection A (with top-level site)
          • Site A.1
            • Site A.1.1
            • Site A.1.2
          • Site A.2
        • Sites collection B (with top-level site)
          • Site B.1
            • Site B.1.1
              • Site B.1.1.1
          • Site B.2
      • Web Application 2
        • Sites collection C (with top-level site)
          • […]
        • Sites collection D (with top-level site)
          • […]

     

    There is a lot things to write about this topic. I hope that you will have an understandable overview of SharePoint infrastructure and also the reasons of getting information from Internet or specialist.



  • SharePoint: How to use SPWebConfigModification class

    SharePoint provides a API that allows developers to modify web.config on all web front-end servers at the same time. This is really a nice concept as soon as it is used the correct way. All web.config modifications should (I could stay must) be performed through the API. Any direct change to web.config files can result in changes lost as this API will override entries in the config file.

    Like for all classes and APIs, developers should have a look to documentation to be able to use the API without bringing any mistakes in their code. Unfortunately, this API is poorly documented. The best documentation is coming from blog posts. There is a lot of posts all over the Internet but you have to read a lot of them if you want a global overview of use of this API. So have a look to this simple feature.

    SPWebConfigModification usual members

    First of all, a web config modification is described with a name, an owner name, an xpath value to define location of the change and a value.

    So if you want to add an entry in the web.config file, you have to know how we can define a unique identifier for the entry. As an example, you want to add <action param1=’value1’ param2=’value2’ param3=’value3’ /> and you know that only param3 should be changed, you have to define Name parameter to action [@param1=’value1’] [@param2=’value2’]. This part is the most obscure part of the SPWebConfigModification class and it is really important to understand why you have to use a correct name so let’s have the explanation: When applying modifications, API will read the web.config file and look for a entry with identifier. If it find such an entry, SPWebConfigModification will replace the current value. If not, it will add an new entry. So, if Name doesn’t fit with config entry, you will have duplicate value in the API.

    Owner property is also really useful. We will use it to know which entries we have to delete when deactivating the feature.

    Feature sample

    In this feature, we will add two entries in the web.config file and remove them when deactivating the feature.If you don’t know how to create a feature, please refer to MSDN Documentation.

    Feature.xml

    <?xml version="1.0" encoding="utf-8"?>
    <Feature  Id="1625EAF0-F3EB-4c4a-AB8A-36FDBD878AE8"
              Title="Company_FeatureName"
              Description="Used to add some entries in the web.config"
              Version="12.0.0.0"
              Hidden="FALSE"
              Scope="WebApplication"
              DefaultResourceFile="core"
              ReceiverAssembly="Company.FeatureName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0a1bc23456d78ef9"
              ReceiverClass="Company.FeatureName.WebConfigModificatorFeatureReceiver"
              xmlns="http://schemas.microsoft.com/sharepoint/">
      <ElementManifests />
    </Feature>

    Feature receiver

        class WebConfigModificatorFeatureReceiver : SPFeatureReceiver
        {
            private const string SPWebConfigModificationOwner = "OwnerName";
    
            public override void FeatureActivated(SPFeatureReceiverProperties properties)
            {
                string name, xpath, value;
    
                SPWebApplication webApp = (SPWebApplication)properties.Feature.Parent;
    
                #region ..: appSettings :..
                name = "add[@key='TrustedGroup']";
                xpath = "configuration/appSettings";
                value = "<add key='TrustedGroup' value='Trusted' />";
                ModifyWebConfig(webApp, name, xpath, value, SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode);
    
                name = "add[@key='KeyName']";
                xpath = "configuration/appSettings";
                value = "<add key='KeyName' value='Value' />";
                ModifyWebConfig(webApp, name, xpath, value, SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode);
                #endregion
     
                try
                {
                    webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
                }
                catch (Exception ex)
                {
                    RemoveAllModifications(properties);
                    throw ex;
                }
            }
    
            public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
            {
                RemoveAllModifications(properties);
    
                try
                {
                    SPWebApplication webApp = (SPWebApplication)properties.Feature.Parent;
                    webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
                }
                catch (Exception ex)
                {
                    System.Diagnostics.EventLog eventLog = new System.Diagnostics.EventLog();
                    eventLog.Source = SPWebConfigModificationOwner;
                    eventLog.WriteEntry(ex.Message);
                    throw ex;
                }
            }
    
            private void RemoveAllModifications(SPFeatureReceiverProperties properties)
            { 
                SPWebApplication webApp = (SPWebApplication)properties.Feature.Parent;
    
                List<SPWebConfigModification> modificationsToRemove = new List<SPWebConfigModification>();
    
                foreach (SPWebConfigModification modification in webApp.WebConfigModifications)
                    if (modification.Owner == SPWebConfigModificationOwner)
                        modificationsToRemove.Add(modification);
    
                foreach (SPWebConfigModification modification in modificationsToRemove)
                    webApp.WebConfigModifications.Remove(modification);
    
                webApp.Update();
            }
    
            private void ModifyWebConfig(SPWebApplication webApp, String nameModif, String pathModif, String valueModif, SPWebConfigModification.SPWebConfigModificationType typeModif)
            {
                SPWebConfigModification modification = new SPWebConfigModification(nameModif, pathModif);
                modification.Value = valueModif;
                modification.Sequence = 0;
                modification.Type = typeModif;
                modification.Owner = SPWebConfigModificationOwner;
    
                try
                {
                    webApp.WebConfigModifications.Add(modification);
                    webApp.Update();
                }
                catch (Exception ex)
                {
                    System.Diagnostics.EventLog eventLog = new System.Diagnostics.EventLog();
                    eventLog.Source = SPWebConfigModificationOwner;
                    eventLog.WriteEntry(ex.Message);
    
                    throw ex;
                }
            }
    
            public override void FeatureInstalled(SPFeatureReceiverProperties properties) { }
    
            public override void FeatureUninstalling(SPFeatureReceiverProperties properties) { }
        }

    Catching exception during ApplyWebConfigModifications

    If you run two ApplyWebConfigModifications simultaneously, an exception will be thrown during the second operation and modifications are not sent (I know, I said  simultaneously, I should say if you execute a ApplyWeConfigModifications before the end of another ApplyWebConfigModifications). Unfortunately, modifications are already in the modifications list but not applied in the web.config files and we will add them again during the next feature activation. Because of this, we will remove all the modifications ourselves before throwing the exception to caller.

    Scope of modifications

    What about the scope? Web config modifications can be applied by web application. To be more precise, this is a web application and all its extensions. It is not possible to apply modifications to a extended web application without applying these modifications to all the extensions.

    Entries conflicts

    Unfortunately, SharePoint doesn’t provide a solution for web.config conflict values (when two applications added the same entry) so when deactivating one of the feature, entry is removed and the second application may not work correctly.

    Entries modifications through UI

    Last but not least, if you want to modify a value without executing any code, you will have to use a page that is able to manage all the entries. You can find such a page here: http://blog.thekid.me.uk/archive/2007/03/24/web-config-modification-manager-for-sharepoint.aspx

    Hope this post will help!



  • Windows 7: How to display .chm content

    When you open a .chm file, you are in front of a Navigation to the webpage was canceled

    image

    First time, I was thinking that .chm was corrupted. It is not the case. In fact, there is a protection on files coming from other computers. You just have to open file properties and click on Unblock button.

    image

    After this, it works… Quite easy but sometimes it can be disturbing.

     image



More Posts
L'auteur du site ne peut être tenu responsable des dommages que les informations fournies pourraient entraîner. Tout est cependant mis en oeuvre pour éviter tout désagrément.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems