Pages

Friday, November 30, 2012

Set Masterpage Poweshell SharePoint



PowerShell commands come handy when you have to perform administrative tasks in your SharePoint environment. In past where you have to write code or a small utility program like a console application or Windows Form application can now be achieved using PowerShell script. . I use this a lot with my clients and since PowerShell script is run by the administrators the IT people love it and don’t have to worry about anything.
In the recent times I have been asked by my clients to provide them with a script to set master pages across multiple site collection in a web application in order to push their branding. I thought that the same script will be useful for a lot of people like me and decided to share the script here by making the script more generic than the one I wrote for my clients.
#Enter the site URL
$siteUrl = Read-Host "Enter Site URL"
$rootSite = New-Object Microsoft.SharePoint.SPSite($siteUrl)
$spWebApp = $rootSite.WebApplication
foreach($site in $spWebApp.Sites)
{
$web = Get-SPWeb $site.Url
$featureFolderNameSite = "PublishingSite"
$featureFolderNameWeb = "PublishingWeb"
$FeatureIDSite = Get-SPFeature -Site $web.Url | Where {$_.DisplayName -eq $featureFolderNameSite}
$FeatureIDWeb = Get-SPFeature -Web $web.Url | Where {$_.DisplayName -eq $featureFolderNameWeb}
# Check Publishing feature is active at site level and site collection level
#if not active activate.
if($FeatureIDSite -ne $null)
{
   Write-Host $featureFolderNameSite "is already activated at :" $web.Url
}

else
{
    Enable-SPFeature -Identity $featureFolderNameSite -Confirm:$false -Url $web.Url
    Write-Host $featureFolderNameSite "has been activated at :" $web.Url
}

if($FeatureIDWeb -ne $null)
{
    Write-Host $featureFolderNameWeb "is already activated at :" $web.Url
 }

else
{
    Enable-SPFeature -Identity $featureFolderNameWeb -Confirm:$false -Url $web.Url
    Write-Host $featureFolderNameSite "has been activated at :" $web.Url
}
$web.CustomMasterUrl = "/_catalogs/masterpage/nightandday.master"
$web.MasterUrl = "/_catalogs/masterpage/nightandday.master"
$web.Update()
$site.Dispose()
}
$rootSite.Dispose()

The script takes the Site URL from the user and loops through all the available site collection present in the web application in which the current site collection is part and sets the master page on all the site collections. The script also activates publishing feature at the site collection level and at site level before setting the master page if it is not activates already.

Tuesday, August 14, 2012

Add Event Handler to Specific List or Library

Event Handlers in SharePoint is the de-facto choice if someone wants to perform some operation synchronously. Some would also point out about Workflows, though workflows can perform the same operation but workflow is acting asynchronously. Suppose you want to set the document or item metadata while adding you would have to use event handler or suppose you want to prevent somebody from deleting an item from a list or library you will be using event handlers. In SharePoint 2010 there are more event handler definition added compared to MOSS 2007. You can find these information here.

In this article let us take a look at how you can register event handlers for a specific list or library. We are focusing on registering it to specific list or library because by default event handlers get registered to all the list or library of particular type. For example if you specify "ListTemplateId=100" event receiver gets registered to all lists that are of type "Custom Lists" Often times you would want the event receiver to be registered to a particular list rather than all the lists in the site. Registering event receivers to a particular List or library give you the flexibility to define an operation specific to the list or library.

There are few different ways you can register event handlers for a list or library.

If you do not worry about event handler getting attached to all the list of a type but just want to control the operation for a specific list you register the event handler on a feature and by checking the List Title you can perform the operation for the specific List.You can use the List Title from properties to prevent event firing for other libraries or lists depending on your type of event receiver.

if(properties.List.Title == "your listname")
{
}

If  you are particular about the event handler getting attached to multiple list and  you like to take control of everything under you then you can use List eventreceiver property to set event receiver for your specific list or library. You can create a feature and in Feature activation you can register the event only for your list. The code below is an example that I use for setting the event handler for Shared Documents library in SharePoint
//This is a web feature

  public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            using (SPSite site = new SPSite(((SPWeb)properties.Feature.Parent).Url))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList sharedDocuments = web.Lists["Shared Documents"];
                    if (sharedDocuments != null)
                    {
                        SPEventReceiverDefinition listeventreceiver =  sharedDocuments.EventReceivers.Add();
                        listeventreceiver.Class = "TestEventReceivers.TestListEventReceiver";
                        listeventreceiver.Assembly = System.Reflection.Assembly.GetExecutingAssembly().FullName; // you can give the assembly name in string format.
                        listeventreceiver.SequenceNumber = 4000;
                        listeventreceiver.Type = SPEventReceiverType.ItemAdded;
                        listeventreceiver.Update();

                    }
                }
            }
        }
In FeatureActivated Method you need to check for existance of event receiver and if not you can add your event receiver. You need to have a class defined for your event receiver.
Event Receiver Example
 public class TestListEventReceiver : SPItemEventReceiver
    {
        public override void ItemAdded(SPItemEventProperties properties)
        {
            base.ItemAdded(properties);
        }
    }

The above operation is similar for assigning event receiver to content type, then use the event in any library or list it is used.

Another way you could assign Event Receiver is by using the list URL. This is a nice and handy way if you are deploying a solution as part of your product or offering and you are creating a List Instance using <ListInstance> in your feature and have defined URL or you already have created the list manually in a site and you have the List URL.

When you create Event Receivers in your solution in Visual studio you will have the following line added

<Receivers ListTemplateId="101">

Replace the List template in the above line as follows. Now you have List instance specific event receiver added.

<Receivers ListUrl="Lists/MyDocuments">
 
I am assigning to a Library in the above example. 
 
 


Thursday, July 12, 2012

SharePoint Designer Workflow


Some improvements and new activity that I would like to see in the upcoming SharePoint

SharePoint as a platform is hugely successful and comparing MOSS 2007 and SharePoint 2010; SharePoint 2010 is more refined and feels more solid as Enterprise Content Management System.  Workflow in SharePoint saw a dramatic improvement compared to MOSS 2007. Today, Designer workflows are definitely considered while making decision about choosing SharePoint as a CMS in the enterprise. But there is an opportunity and huge room available for the workflow to be improved in SharePoint 2012 or SharePoint 15.
In 2010 we got more activities standard in designer workflow and another area that got a facelift is using VISIO as a designer tool for developing workflow. As an enthusiastic SharePoint’er  I wish some of these areas can be improved
1.       Improvement in VISIO designer:
·         Allow configurations to be done in VISIO Designer
·         Enable to preview the working of activities in VISIO
·         Integrate Visio Designer into SharePoint (Having the VISIO outside and have to goto Designer to publish is a sticky point in regulated industry)
·         Provide an easier way to include custom activity with custom icon in VISIO

2.       Improvement in Activities
·         Need more standard OOTB activities VIZ:
·         Activity to Create a Site/Site Collection
·         Activity to Create a Library/List
·         Action to query a list to get list items.
·         Allow calculation of business days in date time activity
·         Get User Information for User Information List
·         Create List Items in different Site (This will be good if we can create list items in different location in the site collection)
·         Convert Document /Infopath to PDF (We can use Document Conversion service but if this is available OOTB then it is huge selling point)
·         New Utility Action to Trim white spaces (This is needed while using Multi Lookup)
·         New utility activity for regular expression operation
·         An Activity to Loop through a collection of Items (List of Values in a multi-lookup field)

Wednesday, July 11, 2012

Content Type Syndication-Part 2


Content Type Syndication (CTS) is a new feature in SharePoint 2010 which is used for Centralization and Sharing of Content Types between site collection and even Farms using Managed Metadata Service application.  For more information on content type syndication and business case for using it please refer here.
In this blog we will see how to configure content type syndication and how to publish and subscribe content types and metadata.

How CTS works :

The figure gives a picture of how content type syndication works and it general architecture.




 
Managed Metadata service acts as a channel to service these content types to the subscriber sites. This is possible because of the service architecture in SharePoint 2010. As shown in the figure the same publisher can be used to publish to different web application using Shared Managed Metadata Service application.
  • ·         Publisher = Content Type Syndication hub
  • ·         Subscriber = is Consumers of published content type

 

How to Configure



Let us now see how we can configure this in our environment. I created a site collection within my web application called “Content Type Syndication Hub”. Please create a site collection in the web application that you want to serve a publisher for content types.







Once you created the site next step in the process is to activate the content type syndication feature at the site collection level. This is to make the current site collection as the hub or source for our consumer sites.







We now have the site collection that can serve content types to any subscriber who wants to consume these content types. But we haven’t provided the channel to serve these content types. We have to create a managed metadata service as conduit.
Let us now see how we can create Manage Metadata Service.
Service application needs to be created in Central Admin 

Application Management >Service Application > Manage Service Applications









Create a New Managed Metadata Service.








Remember to specify the site collection that we created as the content type hub











Now all is set to use the site collection as content type hub and now we need to create a content type and publish it so that consumer site collections can subscribe to the content types from here.
Let us see how we can publish the content type. We need to create a content type for our test purpose and I created one called Test with a site column test”.








We have all we need for the content type to be syndicated to other site collection. This process is pretty straight simple and we will see how we can achieve this.











While you are in Content Type information page click on Manage Publishing for this Content Type, the publishing content type information page will have option to publish and since I have already published my content type while writing this blog I get to see 2 other option to “Unpublish” and “Republish”. If we click ok on the page will publish the content type to all the site collection that is subscribing from t this site collection.








If you now navigate to any site collection that subscribes from this site collection we will be able to see the Content Type published.








If you look at the content type information page of this content type you will have restricted option compared to the content type that is native to this site collection.







If you do not see the content type published as soon as you click ok check your timer job that helps in pushing these updated to all subscriber sites. SharePoint has 2 timer jobs defined that pushes the content type publishing to other sites in the web application. These usually run every hour and you can force it to run immediately if you want it to be pushed immediately.






Well done we have out content type published. Please share your thoughts in comments.