Posts Tagged XML

SharePoint 2013: Create Master Page using Visual Studio 2012

Scenario:

You can find many blogs on how to create SharePoint 2013 master page in Visual Studio 201. We faced following issue while creating master page. Long story, we need to create master pages for our new SharePoint 2013 environment. As with our previous experience, we decided to create them in Visual Studio. Everything works fine but when we try to deploy wsp via Visual Studio 2012, master pages are not uploaded in “Master pages and Layout Library”. The problem was in the XML that was written in “Elements.xml” file.

Resolution:

Before moving to XML that worked, we would like to share all the XML that didn’t worked. We are not sure why they didn’t work. Perhaps, anything wrong on our side.

XML that didn’t work:

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

<Module Name=MasterPages” RootWebOnly=TRUE >

<File Path=MasterPages\ABCIntranet.master” 

Url=_catalogs/masterpage/ABCIntranet.master” 

IgnoreIfAlreadyExists=true >

<Property Name=UIVersion” Value=15></Property>

</File>

</Module>

</Elements>


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

<Module Name=MaterPage” Url =_catalogs/masterpage” List=116>

<File Path=MaterPage\ABCIntranetMasterPage.master” 

Url=ABCIntranetMasterPage.master
Type=GhostableInLibrary” IgnoreIfAlreadyExists=FALSE />

</Module>

</Elements>


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

<Module Url=_catalogs/masterpage” Name=MasterPage>

<File Url=ABCIntranetMasterPage.master” Type=GhostableInLibrary
IgnoreIfAlreadyExists=TRUE” 

Path=MasterPage\ABCIntranetMasterPage.master/>

</Module>

</Elements>

XML that did work is:

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

<Module Name=MasterPage” List=116” Url=_catalogs/masterpage
Path=MasterPage” RootWebOnly=TRUE>

<File Url=ABCIntranet.master” Type=GhostableInLibrary >

<Property Name=UIVersion” Value=15 />

</File>

</Module>

</Elements>

Leave a Comment

SharePoint 2010: Ribbon Customization – Adding button in Editing Tool Tab

Scenario:

When a user click in Content Editor web part, a button appear in the “Editing Tool -> Insert” tab.

Resolution:

Following XML did the trick and we deploy it as feature.

<CustomAction
Id=Ribbon.Add.Button

Location=CommandUI.Ribbon>

    <CommandUIExtension>

      <CommandUIDefinitions>

        <CommandUIDefinition
Location=Ribbon.WikiPageTab.PubPageActions.Controls._children>

          <Button

          Id=Ribbon.WikiPageTab.PubPageActions.AddLinkButton

          Sequence=22

          Image16by16=/_layouts/images/PPEOPLE.GIF

          Image32by32=/_layouts/images/PPEOPLE.GIF         

          Command=AddLinkButton

          LabelText=Add Link Button

          ToolTipTitle=Add Link Button

          ToolTipDescription=Create link.

          TemplateAlias=o2 />

        </CommandUIDefinition>

        <CommandUIDefinition

        Location=Ribbon.WikiPageTab.Scaling._children>

          <MaxSize

            Id=Ribbon.WikiPageTab.Scaling.PubPageActions.MaxSize

           Sequence=15

            GroupId=Ribbon.WikiPageTab.PubPageActions

            Size=LargeLarge />

        </CommandUIDefinition>

      </CommandUIDefinitions>

      <CommandUIHandlers>

        <CommandUIHandler
Command=AddLinkButton
CommandAction=javascript:AddLinkButton();
EnabledScript=javascript:returningtrue(); />

      </CommandUIHandlers>

    </CommandUIExtension>

  </CustomAction>

Leave a Comment

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 (5)