Tuesday, July 22, 2014

Execute server side code on Custom Action: 1.Ribbon click using JavaScript submit 2.Open UrlAction in Modal Dialog

Execute server side code on Custom Action: 1.Ribbon click using JavaScript submit



Ribbon->RibbonServerCode->Elements.xml
--------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction
    Id="RibbonServerCode"
    RegistrationType="List"
    RegistrationId="100"
    Location="CommandUI.Ribbon.ListView">
    <CommandUIExtension>
      <CommandUIDefinitions>
        <CommandUIDefinition
         Location="Ribbon.ListItem.Manage.Controls._children">
          <Button
            Id="Ribbon.ListItem.Manage.Controls.RibbonDemo"
            Alt="Ribbon Server Code"
            Sequence="999"
            Image16by16="/_layouts/Images/Ribbon/Image32x32.jpg"
            Image32by32="/_layouts/Images/Ribbon/Image32x32.jpg"
            Command="RibbonCallServerCode"
            LabelText="Ribbon Server Code"
            TemplateAlias="o1"
            ToolTipTitle="Ribbon Server Code"
            ToolTipDescription="Ribbon server code example."/>
        </CommandUIDefinition>
      </CommandUIDefinitions>
      <CommandUIHandlers>
        <CommandUIHandler
         Command="RibbonCallServerCode"
         CommandAction="javascript:RibbonCallServerCode();"
         EnabledScript="javascript:RibbonEnable();"/>
      </CommandUIHandlers>
    </CommandUIExtension>
  </CustomAction>
  <CustomAction Id="RibbonServerCode.Scripts"
                Location="ScriptLink"
                ScriptSrc="/_Layouts/Ribbon/RibbonServerCode.js" />
</Elements>

Ribbon->Layouts->Ribbon->RibbonServerCode.js
---------------------------------------------------------------
function RibbonCallServerCode() {
    var context = SP.ClientContext.get_current();
    var selectedItems = SP.ListOperation.Selection.getSelectedItems(context);
    var listId = SP.ListOperation.Selection.getSelectedList();
    var itemIds = "";
    for (var i = 0; i < selectedItems.length; i++) {
        itemIds += selectedItems[i].id + ",";
    }
    var pageUrl = SP.Utilities.Utility.getLayoutsPageUrl(
    '/Ribbon/RibbonExecuteServerCode.aspx?ids=' + itemIds + '&listId=' + listId);
    var form = document.createElement("form");
    form.setAttribute("method", "post");
    form.setAttribute("action", pageUrl);
    document.body.appendChild(form);
    form.submit();
    context.executeQueryAsync(Function.createDelegate(this, this.Success), Function.createDelegate(this, this.Failed));
}

function Success() {
}

function Failed(sender, args) {
    alert('request failed ' + args.get_message() + 'n' + args.get_stackTrace());
}

function RibbonEnable() {
    var context = SP.ClientContext.get_current();
    return SP.ListOperation.Selection.getSelectedItems(context).length >= 1;
}

Ribbon->Layouts->Ribbon->RibbonExecuteServerCode.aspx
-------------------------------------------------------------------------------
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>
<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"
    Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RibbonExecuteServerCode.aspx.cs"
    Inherits="Ribbon.Layouts.Ribbon.RibbonExecuteServerCode" DynamicMasterPageFile="~masterurl/default.master" %>

<asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
    <script type="text/javascript">
        function showAlert(redirect_url) {           
            window.location.replace(redirect_url);
        }
    </script>
</asp:Content>
<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
</asp:Content>
<asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
    Application Page
</asp:Content>
<asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea"
    runat="server">
    My Application Page
</asp:Content>


Ribbon->Layouts->Ribbon->RibbonExecuteServerCode.aspx.cs
----------------------------------------------------------------------------------
using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web;

namespace Ribbon.Layouts.Ribbon
{
    public partial class RibbonExecuteServerCode : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string listID = HttpContext.Current.Request.QueryString["listId"].ToString();
            String[] itemIdArray = HttpContext.Current.Request.QueryString["ids"].ToString().Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

            string redirect_url = SPContext.Current.Web.ServerRelativeUrl + "/lists/" + "ImportDataList";

            Page.ClientScript.RegisterStartupScript(this.GetType(), "Call alert function", "javascript: alert('List Id is " + listID + "');", true);

            Page.ClientScript.RegisterStartupScript(this.GetType(), "Call showAlert function", "showAlert('" + redirect_url + "');", true);
        }
    }
}
====================================================================================
Execute server side code on Custom Action: 2.Open UrlAction in Modal Dialog



Ribbon->RibbonServerCode->Element.xml
-----------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction
    Id="RibbonServerCode"
    RegistrationType="List"
    RegistrationId="100"
    Location="CommandUI.Ribbon.ListView">
    <CommandUIExtension>
      <CommandUIDefinitions>
        <CommandUIDefinition
         Location="Ribbon.ListItem.Manage.Controls._children">
          <Button
            Id="Ribbon.ListItem.Manage.Controls.RibbonDemo"
            Alt="Ribbon Server Code"
            Sequence="999"
            Image16by16="/_layouts/Images/Ribbon/Image32x32.jpg"
            Image32by32="/_layouts/Images/Ribbon/Image32x32.jpg"
            Command="RibbonCallServerCode"
            LabelText="Ribbon Server Code"
            TemplateAlias="o1"
            ToolTipTitle="Ribbon Server Code"
            ToolTipDescription="Ribbon server code example."/>
        </CommandUIDefinition>
      </CommandUIDefinitions>
      <CommandUIHandlers>
        <CommandUIHandler
         Command="RibbonCallServerCode"
         CommandAction="javascript:OpenPopUpPageWithTitle('{SiteUrl}/_layouts/Ribbon/RibbonExecuteServerCode.aspx?listId={ListId}&amp;ids={ItemId}', RefreshOnDialogClose, 600, 400,'My Custom Action')"
         EnabledScript="javascript:RibbonEnable();"/>
      </CommandUIHandlers>
    </CommandUIExtension>
  </CustomAction>

  <CustomAction Id="RibbonServerCode.Scripts"
                Location="ScriptLink"
                ScriptSrc="/_Layouts/Ribbon/RibbonServerCode.js" />

  <CustomAction Id="Custom.ProvisionGroup"
                  Location="EditControlBlock"
                  Title="My Custom Action"
                  RegistrationType="List"
                  RegistrationId="100"
                 ImageUrl="/_layouts/images/editicon.gif">
    <UrlAction Url="javascript:OpenPopUpPageWithTitle('{SiteUrl}/_layouts/Ribbon/RibbonExecuteServerCode.aspx?listId={ListId}&amp;ids={ItemId}', RefreshOnDialogClose, 600, 400,'My Custom Action')"/>
  </CustomAction>

</Elements>

Ribbon->Layouts->Ribbon->RibbonServerCode.js
--------------------------------------------------------------------
function RibbonEnable() {
    var context = SP.ClientContext.get_current();
    return SP.ListOperation.Selection.getSelectedItems(context).length >= 1;
}

Ribbon->Layouts->Ribbon->RibbonExecuteServerCode.aspx
--------------------------------------------------------------------------------------
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>
<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"
    Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RibbonExecuteServerCode.aspx.cs"
    Inherits="Ribbon.Layouts.Ribbon.RibbonExecuteServerCode" DynamicMasterPageFile="~masterurl/default.master" %>

<asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
    <%-- <script type="text/javascript">
     function showAlert(redirect_url) {
         window.location.replace(redirect_url);
     }
    </script>--%>
</asp:Content>
<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
    <asp:Button ID="btnClose" runat="server" Text="Close" OnClick="btnClose_Click" />
</asp:Content>
<asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
    Application Page
</asp:Content>
<asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea"
    runat="server">
    My Application Page
</asp:Content>

Ribbon->Layouts->Ribbon->RibbonExecuteServerCode.aspx.cs
----------------------------------------------------------------------------------------
using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web;

namespace Ribbon.Layouts.Ribbon
{
    public partial class RibbonExecuteServerCode : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string listID = HttpContext.Current.Request.QueryString["listId"].ToString();
            String[] itemIdArray = HttpContext.Current.Request.QueryString["ids"].ToString().Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            string redirect_url = SPContext.Current.Web.ServerRelativeUrl + "/lists/" + "ImportDataList";
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Call alert function", "javascript: alert('List Id is " + listID + "');", true);
            //Page.ClientScript.RegisterStartupScript(this.GetType(), "Call showAlert function", "showAlert('" + redirect_url + "');", true);
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            ClosePopUp();
        }

        //Close popup windows
        private void ClosePopUp()
        {
            this.Context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>");
            this.Context.Response.Flush();
            this.Context.Response.End();
        }
    }
}

Thursday, July 17, 2014

Enable or disable ribbon custom action button in SharePoint 2010

Enable or disable ribbon custom action button in SharePoint 2010



<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction
  Id="AlertRibbonButton"
  RegistrationId="100"
  RegistrationType="List"
  Location="CommandUI.Ribbon"
  Sequence="5"
  Title="Alert Ribbon Button">
    <CommandUIExtension>
      <CommandUIDefinitions>
        <CommandUIDefinition Location="Ribbon.List.Settings.Controls._children">
          <Button
          Id="AlertRibbonButtonId"
          Alt="Alert Ribbon Button"
          Sequence="5"
          Command="AlertRibbonButton"
          Image32by32="/_layouts/images/menulistsettings.gif"
          Image16by16="/_layouts/images/itdcl.gif"
          LabelText="Alert Ribbon Button"
          TemplateAlias="o1" />
        </CommandUIDefinition>
      </CommandUIDefinitions>
      <CommandUIHandlers>
        <CommandUIHandler
        Command="AlertRibbonButton"
        EnabledScript="javascript:        
        function getItemIds()
           {
             var itemIds = '';
             var items = SP.ListOperation.Selection.getSelectedItems();
             var item;
             for(var i in items)
             {
               item = items[i];
               if(itemIds != '')
               {
                 itemIds = itemIds + ',';
               }
               itemIds = itemIds + item.id;
             }     
             alert(itemIds);
           }                        
          var EnableDisable = function() {
          this.clientContext = SP.ClientContext.get_current();
          this.selectedItems = SP.ListOperation.Selection.getSelectedItems(this.clientContext);
          var ci = CountDictionary(selectedItems);       
          return (ci > 0);
          //return (ci == 1); //if we want enable for one item only
        };
        EnableDisable();"
        CommandAction="javascript: alert('Hi Alert!');" />
      </CommandUIHandlers>
    </CommandUIExtension>
  </CustomAction>
</Elements>

Featured Post

Sent Email in C#

Sent Email in C# : //Option 1: using S ystem; using System.Net.Mail; using System.Security; using System.Text; using System.Threading.T...

Popular posts