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}&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}&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();
}
}
}
Wonderful illustrated information. I thank you about that. No doubt it will be very useful for my future projects. Would like to see some other posts on the same subject! offshore software outsourcing
ReplyDeleteWow i can say that this is another great article as expected of this blog.Bookmarked this site.. http://45.77.41.36/
ReplyDeleteThanks for taking the time to discuss that, I feel strongly about this and so really like getting to know more on this kind of field. Do you mind updating your blog post with additional insight? It should be really useful for all of us. realistic robot cat
ReplyDeleteReally you have done a good job. Thanks for sharing this valuable information....
ReplyDeleteInplant Training in Chennai
PLC Training in Chennai
Inplant Training in Chennai For CSE
This post is so helpfull and informative.keep updating with more information...
ReplyDeleteDevops Training In Mumbai
Devops Training In Ahmedabad
Devops Course In Cochin
Devops Training In Trivandrum
Devops Training In Kolkata
this post is helpful
ReplyDeletebest plc and scada training in chennai | plc training institute in chennai | best scada training center