Search This Blog

Wednesday, August 31, 2016

Add a custom button SharePoint 2010 ribbon that executes server-side code

In this blog, we learn about the Microsoft SharePoint 2010 ribbon by adding or removing a custom button that executes server-side code.

To add button into ribbon follow below steps:

1.     Click Microsoft Visual Studio 2010.



2.     On the File menu, point to New, and then click Project.



3.     In the New Project dialog window, in the Installed Templates section, click Visual C#, click SharePoint, and then click 2010.

4.     Click Empty SharePoint Project from the project items.

5.     In the Name box, type RibbonDemo and then click OK.



6.     In the SharePoint Customization Wizard, type the local Web site that you want to use for this exercise (such as http://localhost/SampleWebSite).

7.     For the trust level, select Deploy as a farm solution and then click Finish.




8.     In Solution Explorer, right-click the RibbonDemo node, point to Add, and then click New Item.



9.     In the Add New Item dialog screen, in the Installed Templates section, click Visual C#, click SharePoint, click 2010, and then click the Empty Element item type.



10.  Leave the Name as EmptyElement1 and then click OK.

11.  In Solution Explorer, right-click the EmptyElement1 node and then click View Code.

12.  Delete the contents of the Elements.xml file.

13.  Add the following code to the Elements.xml file.



<?xml version="1.0encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<CustomAction
  Id="DemoHelloWorldButton"
  RegistrationType="List"
  RegistrationId="101"
  Location="CommandUI.Ribbon">
    <CommandUIExtension>
      <CommandUIDefinitions>
        <CommandUIDefinition
         Location="Ribbon.Documents.New.Controls._children">
          <Button
           Id="Ribbon.Documents.New.Controls.DemoHelloWorldButton"
           Alt="Hello World Ribbon Button"
           Sequence="10"
           Image32by32="/_layouts/images/PPEOPLE.GIF"
           Command="Demo_HelloWorld"
           LabelText="Hello World Demo"
           TemplateAlias="o2"/>
        </CommandUIDefinition>
      </CommandUIDefinitions>
      <CommandUIHandlers>
        <CommandUIHandler
         Command="Demo_HelloWorld"
         CommandAction="javascript:alert('Hello World!');/>
      </CommandUIHandlers>
    </CommandUIExtension>
  </CustomAction>
</Elements>


14.   In Solution Explorer, right-click the RibbonDemo node and then click Deploy.

15.   Open the Web site specified previously.

16.   Click the Documents tab in the SharePoint 2010 ribbon.




17.   You should see the new Hello World Demo button added to the ribbon.
Click Hello World Demo. The Hello World JavaScript dialog box appears as shown Figure


To remove button into ribbon follow below steps:
1.       In Solution Explorer, right-click the EmptyElement1 node and then click View Code.

2.       Comment the existing code by typing <!-- in front of the opening <CustomAction> element. Next, type --> after the closing </CustomAction> element.

3.       After the commented code, insert the following code.


 
 
<HideCustomAction Id="Ribbon.Documents.New.Controls.DemoHelloWorldButton" 
Location="Ribbon.Documents.New.Controls._children">
</HideCustomAction>

4.       Deploy and test the updated solution by following steps 1 through 4 in Task 3, Deploy and Test the Solution.

Handling event of SharePoint form dropdown value change


Problem Description:

Many times we want to perform event handling on SharePoint list’s form.
This blog will give us idea about “Choice” column value change event handling.

As usual add Content Editor WebPart into SP list form and copy paste below code into HTML form.


<script type="text/javascript">

_spBodyOnLoadFunctionNames.push("ourFunctionName");

function ourFunctionName()
{

$("Select[Title='ColumnName']").change(function () {
alert("Choice column value changed");
});
}

</script>

Reference:



Remark:

For Date and Time column is quite different, for that follow below link,


Set SharePoint form field show, hide or read-only using JavaScript


Sometimes from validation point of view we need to hide, visible or read only SharePoint List from fields.This blog will give us idea about how to do that using JavaScript.

·         Set SharePoint form field read-only:

<script type ="text/javascript">
function SetReadOnly()
{
var elements=document.getElementById('ctl00_m_g_adee8cfa_dbff_48bf_b75c_ae50351ebbf7_ff21_ctl00_ctl00_TextField');
elements.readOnly=true;
}
_spBodyOnLoadFunctionNames.push("SetReadOnly()");
</script>


·         Hide SharePoint form field:

$("# ColumnID").hide();


·         Show SharePoint form field:

$("#ColumnID").show();

Remark: Here "ColumnID" is id for that particular column specify in <tr> tag on SharePoint form  
Eg. <tr id=”ColumnID”>

Example:
·         show and hide SharePoint form field base on dropdown value change in sharepoint

<script type="text/javascript">
_spBodyOnLoadFunctionNames.push("ourFunctionName");

function ourFunctionName()
{
$("Select[Title='Type']").change(function () {
var val=$("Select[Title='Type']").val();

if(val=="Pre")
{
$("#Post").hide();
$("#Pre").show();
}
else if(val=="Post")
{
$("#Post").show();
$("#Pre").hide();
}
});
}
</script>

SharePoint number column should accept only numbers



Solution:

<script type="text/javascript">
        $(document).ready(function () {
            $('input[Title="ColumnName"]').keydown(function (e) {
                var keyCode = e.which; // Capture the event
                //190 is the key code of decimal if you dont want decimals remove this condition keyCode != 190
                if (keyCode != 8 && keyCode != 9 && keyCode != 13 && keyCode != 37 && keyCode != 38 && keyCode != 39 && keyCode != 40 && keyCode != 46 && keyCode != 110 && keyCode != 190) {
                    if (keyCode < 48) {
                        e.preventDefault();
                    }
                    else if (keyCode > 57 && keyCode < 96) {
                        e.preventDefault();
                    }
                    else if (keyCode > 105) {
                        e.preventDefault();
                    }
                }
            });
        });
       
</script>

SharePoint 2013 & 2010 reference books

Microsoft SharePoint 2013: Designing and Architecting Solutions


Microsoft SharePoint 2013: Planning for Adoption and Governance

Exam Ref 70-331: Core Solutions of Microsoft SharePoint Server 2013

Exam Ref 70-332: Advanced Solutions of Microsoft SharePoint Server 2013

SharePoint 2013 User's Guide, 4th Edition

Developing Business Intelligence Apps for SharePoint

Pro SharePoint 2013 Business Intelligence Solutions

Pro SharePoint Disaster Recovery and High Availability, 2nd Edition

Pro SharePoint 2013 App Development

SharePoint 2013 Branding and User Interface Design

SharePoint 2013 For Dummies

Business Intelligence in Microsoft SharePoint 2013

Pro SharePoint 2013 Administration, 2nd Edition

Microsoft SharePoint 2013 Inside Out

Microsoft SharePoint 2013: Designing and Architecting Solutions

Exploring Microsoft SharePoint 2013

Beginning SharePoint 2013

============================= SharePoint 2010 =============================

Pro SharePoint 2010 Development for Office 365

Practical SharePoint 2010 Branding and Customization

Working with Microsoft FAST Search Server 2010 for SharePoint

Practical SharePoint 2010 Information Architecture

Pro SharePoint with jQuery

Microsoft SharePoint 2010 Business Application Blueprints

SharePoint Server 2010 Administration 24 Hour Trainer

SharePoint bloggers and their blogs


NameSite
Amardeep Singhhttp://mossnwss.blogspot.co.uk/
Mai Omar Desoukihttp://maiomardesouki.com/
Amol ghugehttp://sharepointknowledgebase.blogspot.in/
Marc Andersonhttp://sympmarc.com/
 http://www.sympraxisconsulting.com/Demos/default.aspx
Asif Rehmanihttp://blog.sharepoint-videos.com/
 http://www.learningsharepoint.com/
 http://www.akruratechnologies.com/blog/
Usman Ahmedhttp://www.sharepoint.inf4web.com/category/sharepoint/
 http://sharepointgeorge.com/
 http://khm-tech-talk.blogspot.in/
Erik Swensonhttp://erikswenson.blogspot.in/
Ken Pricehttp://thesharepointblog.com/
Devendra Velegandlahttp://www.sharepoint-journey.com/
Rahul Saxenahttp://www.dotnetcodesg.com/SharePoint/
Tobias Zimmergrenhttp://zimmergren.net/
Naupliushttp://thesharepointfarm.com/
 http://blog.metrostarsystems.com/tag/sharepoint/
 http://www.sharepointsiren.com/
Randel Hallhttp://thecloudengineer.blogspot.in/
Donal Conlonhttp://donalconlon.wordpress.com
Bjørn Furuknaphttp://blog.furuknap.net/
Paolo Pialorsihttp://www.sharepoint-reference.com/Blog/default.aspx
Jeremy Thakehttp://www.jeremythake.com/
Todd Klindthttp://www.toddklindt.com/default.aspx
 http://weblogs.asp.net/bsimser/
Cornelius J. van Dyk'shttp://www.cjvandyk.com/
Jason Himmelsteinhttp://blog.sharepointlonghorn.com/
Jimmy Bourquehttp://get-spchopps.com/
Laura Rogershttp://www.wonderlaura.com/
Lori Gowinhttp://www.pointgowin.com/seethepoint/default.aspx
Maurice Pratherhttp://www.bluedoglimited.com/SharePointThoughts/
Shane Younghttp://msmvps.com/blogs/shane/
Tom Resinghttp://tomresing.com/
Mark Rackleyhttp://sharepointhillbilly.com/default.aspx
Heather Watermanhttp://heatherwaterman.com/category/sharepoint-2010/
Paul Schaefleinhttp://sharepoint.mindsharpblogs.com/Paul/default.aspx
Cathy Dew akahttp://www.sharepointcat.com/
Nishant Ranahttp://nishantrana.wordpress.com/category/sharepoint/
Dipti Chhatrapatihttp://sharepointrun.wordpress.com/
Bjoern H Rapphttp://www.sharepointviking.com/
Andy Talbothttp://www.sharepointandy.com/