Jul 15, 2012

Adding a Custom Button to SharePoint Ribbon

Hi Guys,

In this post I am going to explain you how a custom button can be added to the SharePoint Ribbon and how to raise an event with it. Here I am planning to add a custom button with an event to open a application page.

These are the steps involved in this

  • Create a Empty SharePoint project
  • Add a Feature to it
  • Add an Empty Element to project
  • Add an Application page to project
  • Add a Image to the project 
  • Define the schema for Elements.xml 

Open Visual Studio 2010 and create a new Empty Sharepoint Project (Here I am deploying as a farm solution) and named it as My Ribbon.

Now add a feature to the project


and name it as My Ribbon Demo Feature (also give description if you need )


  Then add empty element to the project


  Then select empty element and give name as MyRibbon Element

Then you will get an Elements.xml with this empty element like this
Now we need to add the schema/configuration stuff in to this xml file.

As I explained earlier here my objective is to add a custom button to the ribbon with an event to open the custom application page.

 So now add an application page to the solution. Select the Project then say add new item



Then Select the application page and name it as ApplicationPage1.aspx

Now our solution structure will be look like this



Add a Image to the project for the button. Select Project then add SharePoint Images Mapped Folder


Then a folder with project name(MyRibbon) is added to the solution then add an image to it .


For this select MyRibbon folder and add existing item then upload the image. Here i am uploading from my pc and its name is pcm.jpg(text:New)




Now write the below lines in to the Elements.xml file . Replace elements tag with this.


<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction Id="DemoHello" RegistrationType="List" RegistrationId="101"
                Location="CommandUI.Ribbon">
    <CommandUIExtension>

      <CommandUIDefinitions>
        <CommandUIDefinition Location="Ribbon.Documents.New.Controls._children">
           <Button Id="Ribbon.Documents.New.Controls.DemoHelloWorldButton" Alt="purnaImage"
                 Image32by32="/_layouts/images/MyRibbon/pcm.jpg"
                         Command="Mydemo_Command" LabelText="MyButton" TemplateAlias="o2"></Button>
        </CommandUIDefinition>
      </CommandUIDefinitions>

      <CommandUIHandlers>
        <CommandUIHandler Command=" Mydemo_Command"
                          CommandAction="javascript:
        var dialogOptions = {
          url: '/_layouts/MyRibbon/ApplicationPage1.aspx',
          title: 'Custom Ribbon Button PopUp',
          allowMaximize: true,
          showClose: true,
          width:500,
          height:400
        };
        SP.UI.ModalDialog.showModalDialog(dialogOptions); "  >
        </CommandUIHandler>
      </CommandUIHandlers>

    </CommandUIExtension>
  </CustomAction>
</Elements>

Once it is done deploy the solution.

Check the feature status.

Open the site (where we deployed the solution). Site ActionsSite SettingsManage Site Features and then check our feature activated or not.



Now check the custom button in the ribbon . Go to Shared Documents(or any library) then select a item/folder in it. You will see our custom button added as a tab in the ribbon with My Button as text (see below fig)



Now check the event for it.Click the button, our applicationpage1.aspx will be opened.


Now only thing we need to concentrate here is the lines which were written in elements.xml file.

The elements.config file contains different sections and some of them are here..
Custom Action : This element tells us that which custom action we are writing/adding to sharepoint. The location atribute defines the location of the sharepoint element(like ribbon tab etc..)
CommandUIExtension : It contains the information about the controls and the actions of them.
CommandUIdefination : It contains the definition of control like its properties.
CommandUIHandler : This deals with the action/work of the control.


For more information about these tags refer the post from  msdn.
Happy reading..!

Jun 6, 2012

HTML Editor/ Rich Text Editor Style Customization in SharePoint Ribbon

Hi All,

In this post I am explaining you how to customize the default markup styles in HTML Rich Text Editor of SharePoint 2010 Ribbon. We can change or remove the existing styles and as well as we can add a new custom style . By default we will get these styles from the Mark up Styles Button in the Rich Text Editor pane (Ribbon) as shown below.



Here I am removing the default style i.e Heding2 Style.For this go to the style sheet which you are using for the site (by default COREV4.CSS)

This will be located under the path

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\1033\STYLES\Themable

Open COREV4.css and find the class ms-rteElement-H2 and remove it

H2.ms-rteElement-H2

{
-ms-name:"Heading 2";
}
.ms-rteElement-H2
{
font-size:1.3em;
font-weight:normal;
}

Now save it and check the site, the Heading 2 is removed



In the similar way If we want to add a new style (our custom style)
H4.ms-rteElement-H4mystyle
{
-ms-name:"MY CUSTOM STYLE";
}
.ms-rteElement-H4mystyle
{
font-size:15px;
font-weight:bold;
color: red;
}

Here I added a style with the properties like font-size of 15px ,font-weight bold and forecolor red. I added this class under CallOut 4 class. So that will be appear under callout4 style as shown with the specified styles like this





Apr 10, 2012

Downloading Wsp from SharePoint Central Admin


Recently I got a requirement to check the contents of a wsp which was already deployed on the Client's SharePoint server.  I have given access to Central Admin, but as you know SharePoint will not provide us any link to download/extract the deployed wsp. 

These are the ways I found on the web to download/extract the wsp..

  • Using Power Shell Script
  •  Using Code

1. Using PowerShell script :




$myFarm = Get-SPFarm
$myWSP = $myFarm.Solutions.Item("documentspacenew.wsp").SolutionFile
$ myWSP.SaveAs("c:\purna\ documentspacenew.wsp")

 

This will be saved as 



 
2.  Using Code :

  protected void btnGetWSP_Click(object sender, EventArgs e)
        {
            SPSolutionCollection allSolutions = SPFarm.Local.Solutions;
            foreach (SPSolution mySolution in allSolutions)
            {
                SPPersistedFile myWSP = mySolution.SolutionFile;
                if (myWSP.Name == "documentspacenew.wsp")
                {
                    myWSP.SaveAs("c:\\Purna\\" + mySolution.Name);
                }
            }
        }

Here the SPSolutionCollection , SPPersistedFile classes are referred from   Microsoft.SharePoint.Administration namespace.

Finally I have got the contents of the downloaded solution by changing the extension from .wsp to .cab.
 

Hope Helpful..!

Apr 9, 2012

Adding Webpart to the Page Programatically in SharePoint

Hi All,

Here I am explaining you how to add a web part to the page programatically.  For this demo I have created a web part page called Demo.aspx  and I am adding a content editor web part to this page through programatically. 

For this first we need to get the reference of the SPLimitedWebPartManager  of the page where we are going to add the webpart.

Here is the code
protected void Addwebpart_Click(object sender, EventArgs e)
        {           
            using (SPSite site = new SPSite(SPContext.Current.Site.ID))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    web.AllowUnsafeUpdates = true;
                    //getting reference of SPLimitedWebpartManager of the page
                    SPLimitedWebPartManager wpManager = web.GetLimitedWebPartManager("http://sp2010:9999/SitePages/Demo.aspx", System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);                   
                    //Creation of ContentEdiotr Webpart
                    ContentEditorWebPart myCEWP = new ContentEditorWebPart();
                    myCEWP.ID = "myCEWP";
                    myCEWP.Title = "Demo Content Editor";
                    //Adding Webpart to the page
                    wpManager.AddWebPart(myCEWP, "Main", 1);
                    wpManager.SaveChanges(myCEWP);
                }
            }

        }

If you want to add the Shared Documents (ListView webpart) to the page,  Here is the way..

                    web.AllowUnsafeUpdates = true;
                    SPList lstCDD = web.Lists.TryGetList("Shared Documents");
                    if (lstCDD != null)
                    {
                        SPLimitedWebPartManager wpManager = web.GetLimitedWebPartManager("http://sp2010:9999/SitePages/Demo.aspx", System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

                        XsltListViewWebPart webpartCDD = new XsltListViewWebPart();
                        webpartCDD.ListName = lstCDD.ID.ToString("B").ToUpper();
                        webpartCDD.ViewGuid = lstCDD.DefaultView.ID.ToString("B").ToUpper();
                        wpManager.AddWebPart(webpartCDD, "Main", 2);
                        wpManager.SaveChanges(webpartCDD);

                    }

Here the AddWebpart method expects three parameters, webpart, zoneid(string-Id of the webpartzone where the webpart is being added to like Main, Left etc), zone index(Int-Index of the zone like 1,2,3..)

Cheers,

Jan 20, 2012

Attaching specific W3WP Worker Process in SharePoint Custom Code Debugging


Hi Guys,

This is a small post but very useful  in the sharepoint  custom application development.

While debugging your custom code in the SharePoint instead of attaching all worker processes(w3wp.exe) this is the command to attach/identify only the current application specific w3wp process

Command to get the correct w3wp process

C:\wondows\System32\inetsrv\appcmd list wp




 
Here I have written my custom code on Sharepoint-1122 site and debugging the same. So the processor which attached to this application  is 4736(see above fig).

So attach only one process which is associated (like below) instead all




Have a happy debugging..!