Archive for Feature

SharePoint 2010: PowerShell Script to Create SiteCollection using Custom Template

Scenario:

We need to write PowerShell script to create site collection using custom template.

Resolution:

First, we need to save site as template. For this, “Site Settings” -> “Save site as template”. Enter site template name, say, testCustomtemplate. After saving, go to “Solution Gallery” and download the testCustomtemplate.wsp.

Second, add the wsp. For this, run SharePoint PowerShell and run following commands:

Add-SPSolution [Path to testCustomtemplate.wsp]

Install-SPSolution –identity testCustomtemplate.wsp

Third, run following script making appropriate changes:

$newSite = New-SPSite -Name [SiteCollectionName] -Url [URL] -OwnerEmail [OwnerMail] -OwnerAlias [OwnerLogin] -SecondaryOwnerAlias [SecondaryOwnerLogin]

Enable-SPFeature -Identity [FeatureGuid] -Url $newSite.Url

$rootWeb = $newSite.RootWeb

$rootWeb.ApplyWebTemplate(“[CustomTemplate]”)

You need to find values of two variables. One is “FeatureGuid” and other is “CustomTemplate”.

FeatureGuid can be found out by simply going to 14 hives, go for folder containing the name “WebTemplate” along with your solution name and copy feature ID. Like we have feature ID {e27e2916-5eb2-4a23-901b-fb3f386136bb}.

CustomTemplate can be manually created by concatenating the name of the solution and feature as under:

{e27e2916-5eb2-4a23-901b-fb3f386136bb}# testCustomtemplate

Or run following simple script after activating the feature on site collection:

$site = Get-SPSite [SiteCollectionURL]

$web = $site.RootWeb

$templates = $web.GetAvailableWebTemplates(1033) //1033 is Culture ID.

Foreach($temaplte in $templates)

{

$template.Name

}

Advertisements

Comments (2)

SharePoint 2007: Remove / Disbale Anonymous Access From Browsing Application Pages / Document Library

Issue:

We enable anonymous access of one of our site collections. The concern was that anonymous users were able to access application pages of the site. Like anonymous users were able to browse “All Site Contents” by adding “_layouts/viewlsts.aspx” to the URL. This also give them ability to browse document libraries, lists etc.

Resolution:

First solution is in two steps:

  1. Enable LockDown Feature using command:

    stsadm –o activatefeature -filename ViewFormPagesLockdown\feature.xml –url <SiteCollectionURL>

  2. Disbale and enable anonymous access by setting it to “Nothing” and then again back to “Entire Web Site”.

Second solution is add following lines in web.config where <location=path…> exist:

<location path=”_layouts/viewlsts.aspx”>

<system.web>

<authorization>

              <deny users=”?” />

      </authorization>

      </system.web>

</location>  

This will block user to view “View All site Contents”.

One interesting point is that if you want to search how many SharePoint site are there with this issue, try google “This system library was created by the Publishing feature to store pages that are created in this site.”

Source:

http://blogs.msdn.com/ecm/archive/2007/05/12/anonymous-users-forms-pages-and-the-lockdown-feature.aspx

http://technet.microsoft.com/en-us/library/cc263468.aspx

http://www.agileconcepts.com/Blogs/AQ/Lists/Posts/Post.aspx?List=71ced1e4%2Dd1b5%2D4ce4%2D9cb7%2D209f38df315a&ID=10&RootFolder=%2A

http://blog.richfinn.net/blog/2008/07/22/PublicFacingSharePointHeresSomethingScaryPleaseActivateViewFormPagesLockDown.aspx

Comments (2)

Adding an Event Handler to a Feature in MOSS

We will try to provide as much detail as possible while trying to explain how to write an event handler, but if you find difficulty in understanding event handler concepts, explore the this link . We will use the feature added in the previous post 

Open the project “HelloWorldFea” and rename Class1.cs to “FeatureReceiver.cs”. Add the reference of Micrososoft.SharePoint.dll. (if you don’t know how to add, click here). Write the following code in “FeatureReceiver.cs” file. 

using Microsoft.SharePoint; 

namespace HelloWorldFea{   

public class FeatureReceiver : SPFeatureReceiver    { 

public override void FeatureActivated(SPFeatureReceiverProperties properties)       

{           

SPWeb site = (SPWeb)properties.Feature.Parent;           

site.Properties[“OriginalTitle”] = site.Title;           

site.Properties.Update();            

site.Title = “HELLO WORLD”;           

site.Update();               

}

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)       

{           

SPWeb site = (SPWeb)properties.Feature.Parent;           

site.Title = site.Properties[“OriginalTitle”];           

site.Update();       

} 

public override void FeatureInstalled(SPFeatureReceiverProperties properties)       

{

} 

public override void FeatureUninstalling(SPFeatureReceiverProperties properties)        

{                   

}   

}

} 

We have inherited form the class SPFeatureReceiver and override four methods which are FeatureActivated, FeatureDeactivating, FeatureInstalled and FeatureUninstalling. FeatureActivated will fire after a feature is activated and FeatureDeactivating will fire before a feature is deactivated. 

In FeatureActivated method, we did a simple trick. We store site name using persistent property bag and replace it with “HELLO WORLD” title. When the feature is deactivated, the original title of the site is restored. 

Now it’s time to deploy the assembly. First we sign the assembly. (if you don’t know how to sign, click here). The next step is to deploy the assembly into GAC. We can do that by moving to the properties of the project and then to “Build Events” tab. “Post build event command line window:” text box will already have following lines: 

cd $(ProjectDir)

Install.bat 

(For details, check this post) 

Add the following lines in the beginning. 

“%programfiles%\Microsoft Visual Studio 8\SDK\v2.0\Bin\gacutil.exe” /if $ “(TargetPath)” 

The last step is to add the reference in the “feature.xml” file. The updated look of the file is: 

<Feature 

Id=“” 

Title=HELLO WORLD FEATURE 

Description=This is my very first custom feature 

Version=1.0.0.0  

Scope=Web 

Hidden=FALSE 

ImageUrl=menuprofile.gif 

ReceiverAssembly=HelloWorldFea, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9136ecc8446e96dc  ReceiverClass=HelloWorldFea.FeatureReceiver  

xmlns=http://schemas.microsoft.com/sharepoint/>   

<ElementManifests>   

<ElementManifest Location=elements.xml /> 

</ElementManifests>

</Feature> 

If you don’t have the idea about the ReceiverAssembly / ReceiverClass element, click here).  Activate and deactivate the feature to test the working of the event handler. If not working, try doing the things manually.

 

Comments (2)

CREATING AND DEPLOYING FEATURE IN MOSS

Windows SharePoint Services 3.0 introduces an inherently portable and modular functionality known as feature, which provides a mechanism for defining site elements. 

 A feature is a package of Windows SharePoint Services elements that can be activated for a specific scope and that helps users accomplish a particular goal or task. 

 Features are added to a target site or site collection through a process known as feature activation. The element types that can be defined by a feature include menu commands, link commands, page templates, page instances, list definitions, list instances, event handlers, and workflows. 

 So let’s start writing a feature. The functionality of the feature which we are going to develop is that it adds a menu item “Hello World” in the “Site Actions” menu. Clicking on the menu item “Hello World” will transfer to Google. Our goal is shown in the figure below:

 

 6-3.jpg

 

 The first step is to understand the feature file structure. If you go to “C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES” directory, you will see features already existed. Each feature has a file “feature.xml” and other file usually named as “elements.xml”. 

 Now we move to VS 2005 and create a new class library DLL project and name it “HelloWorldFea. Now, it will be intelligent to create the same structure of files as that of WSS 3.0 in the VS 2005. 

 Create a directory “TEMPLATE” in the solution. Inside the “TEMPLATE” directory, create another directory “FEATURE” and lastly, in the “FEATURE” directory, create directory with the same name as that of the project which happens to be “HelloWorldFea”. 

 

6-1.jpg 

 

Now, create a xml file and name it “feature.xml” inside the directory “HelloWorldFea”. In this xml file, we will write the details that will define the high level attributes of the features itself. Add the following XML to the “feature.xml” file. 

<Feature 

Id=“” 

Title=HELLO WORLD FEATURE 

Description=This is hello world feature. 

Scope=Web 

Hidden=FALSE 

ImageUrl=menuprofile.gif 

xmlns=http://schemas.microsoft.com/sharepoint/>   

    <ElementManifests>   

         <ElementManifest Location=elements.xml /> 

    </ElementManifests>

</Feature>  

We describe the attributes of the Feature tag as under: 

Id: GUID which identifies the features uniquely. It can be created by clicking “Tools” menu of VS 2005 and then on “Create GUID”. “Create GUID” window will appear. Select “4 Registry Format (ie. (xxxx …))” option. Click on “Copy” button and paste the GUID against the Id attribute. 

Title: Specifies the title of the feature. 

Description: Specifies the description of the feature. 

Scope: Scope defines the context in which feature is activated and deactivated. It can have four values namely Web, Site, WebApplication and Farm. Web specifies that the feature can be activated and deactivated within the context of site. Site specifies that the feature can be activated and deactivated within the context of the site collection. Same stands for WebApplication and Farm. 

Hidden: Can be set to true or false. In case of true, feature is not visible in the list of available features. It can be activated or deactivated through command line, through custom code, or through an activation dependency with another feature. 

ImageUrl: The image displayed next to the feature in the use interface. The last part is ElementManifests element. The element has inner element ElementManifest which specifies the reference of other XML file. This file defines other elements that make up the feature. In our case, we have one file “elements.xml”. 

Create “elements.xml” file in the “HelloWorlFea” directory. The XML for the file is as under: 

<Elements xmlns=http://schemas.microsoft.com/sharepoint/ >  <CustomAction    Id=SiteActionsToolbar     

 

GroupId=SiteActions   

Location=Microsoft.SharePoint.StandardMenu   

Sequence=100   

Title=Hello World   

Description=A custom menu item added using a feature   

ImageUrl=_layouts/images/menuprofile.gif >   

    <UrlAction Url=http://www.google.com/>     

</CustomAction>

</Elements > 

Id has the name of the menu in which we want to add our menu item. Title and description specifies the text that will appear as title and description. UrlAction states the url of the site where user is redirected when clicked. In our case, it is http://www.google.com. 

The next step is to deploy the feature unto the “FEATURE” directory. For this purpose we create a batch file and name it “deploy.bat”. The contents of the file are: 

@SET TEMPLATEDIR=”C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE”

@SET STSADM=”C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN\stsadm” 

Echo Copying filesxcopy /e /y TEMPLATE\* %TEMPLATEDIR% 

Echo Installing feature%STSADM% -o InstallFeature -filename HELLOWorldFea\feature.xml -force 

Echo Reatart IIS Worker Process

IISRESET  

In the end, go to the properties of the project and then to “Build Events” tab. Write the following lines in the “post build event command line window:”  

cd $(ProjectDir)

install.bat 

The first line is required to change the current directory to that of the project directory. The second line runs the batch file to copy the feature files to the correct location and install the feature with the InstallFeature operation of the command-line STSADM.EXE utility. Now go to the “Site Features” page by moving from “Site Actions” to “Site Settings” and clicking on “Site Features” of your site. Here, “Hello World Feature” will be available. Activate it and you will be able to see a menu item added in the “Site Actions” menu. If you fail to find the feature, check the following steps: 

  • “HelloWorldFea” directory is added in the FEATURES directory at the path “C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES”

 

  • Run the file “feature.xml” and “element.xml” to check that XML is error free.

If still unable to find the feature, try to deploy manually and see what happen. 

http://msdn2.microsoft.com/en-us/library/ms439657.aspx

Source:

http://www.microsoft.com/learning/en/us/book.aspx?ID=9692&locale=en-us

Comments (6)