Adding Datasheet view in SharePoint2013

Add below to DatSheetvw.aspx.cs

/*start*/

public partial class DatasheetV : LayoutsPageBase
{
protected PlaceHolder plActiveX;
protected LinkButton linkList;

protected void Page_Load(object sender, EventArgs e)
{
}

protected override void CreateChildControls()
{
base.CreateChildControls();
StringBuilder stringBuilder = new StringBuilder();
try
{
string input1 = this.Page.Request[“List”];
SPList spList;
try
{
spList = SPContext.Current.Web.Lists[Guid.Parse(input1)];
}
catch
{
spList = SPContext.Current.Web.Lists[input1];
}
this.linkList.Text = spList.Title;
SPView view = spList.DefaultView;
string input2 = this.Page.Request[“View”];
if (!string.IsNullOrEmpty(input2))
{
Guid result = Guid.Empty;
if (Guid.TryParse(input2, out result))
{
foreach (SPView spView in (SPBaseCollection)spList.Views)
{
if (spView.ID == result)
{
view = spView;
break;
}
}
}
}
this.linkList.PostBackUrl = view.ServerRelativeUrl;
SPListItemCollection items = spList.GetItems(view);
stringBuilder.AppendFormat(“”, (object)spList.ID.ToString(), (object)view.ID.ToString(), (object)SPContext.Current.Web.Url, (object)SPEncode.HtmlEncode(items.Xml), (object)SPEncode.HtmlEncode(view.SchemaXml), (object)SPEncode.HtmlEncode(spList.SchemaXml));
}
catch (Exception ex)
{
stringBuilder.Append(((object)ex).ToString());
}
this.plActiveX.Controls.Add((Control)new Literal()
{
Text = (“<script>viewServerRelativeUrl = \”” + this.linkList.PostBackUrl + “\”;RenderActiveX(\”” + SPEncode.ScriptEncode(((object)stringBuilder).ToString()) + “\”);</script>”)
});
}

private void b_Click(object sender, EventArgs e)
{
}
}

/*end*/

Add below to Datsheetvw.aspx

<%@ Assembly Name=”$SharePoint.Project.AssemblyFullName$” %>
<%@ Import Namespace=”Microsoft.SharePoint.ApplicationPages” %>
<%@ Register Tagprefix=”SharePoint” Namespace=”Microsoft.SharePoint.WebControls” Assembly=”Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” %>
<%@ Register Tagprefix=”Utilities” Namespace=”Microsoft.SharePoint.Utilities” Assembly=”Microsoft.SharePoint, Version=15.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=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” %>
<%@ Page Language=”C#” AutoEventWireup=”true” CodeBehind=”Datasheet.aspx.cs” Inherits=”EGIT.ECMDataSheetView.Layouts.EGIT.ECMDataSheetView.DatasheetV,EGIT.ECMDataSheetView,Version=1.0.0.0, Culture=neutral, PublicKeyToken=3a3922901ef30954″ DynamicMasterPageFile=”~masterurl/default.master” %>

<asp:Content ID=”PageHead” ContentPlaceHolderID=”PlaceHolderAdditionalPageHead” runat=”server”>
<script>
var viewServerRelativeUrl = “/”;
function StopEditing() {
try {
window.location.href = viewServerRelativeUrl;
} catch (e) { }
}
</script>
</asp:Content>

<asp:Content ID=”Main” ContentPlaceHolderID=”PlaceHolderMain” runat=”server”>

<table border=”0″ cellpadding=”0″ cellspacing=”0″>
<tr>
<td class=”ms-list-addnew ms-textXLarge ms-list-addnew-aligntop ms-soften”>
<a title=”Stop editing and save changes.” onclick=”StopEditing();return false;” href=”javascript:;”>Stop</a> editing this list
</td>
</tr>
</table>

<asp:PlaceHolder ID=”plActiveX” runat=”server”></asp:PlaceHolder>

<script type=”text/javascript”>
function WPQ2ShowHideTaskPane() {
GCShowHideTaskPane(document.STSListControlWPQ2);
}

function WPQ2ShowHideTotalsRow() {
GCShowHideTotalsRow(document.STSListControlWPQ2);
}

function WPQ2Refresh() {
GCRefresh(document.STSListControlWPQ2);
}

function WPQ2GridNewRow() {
GCGridNewRow(document.STSListControlWPQ2);
GCActivateAndFocus(document.STSListControlWPQ2);
}

function WPQ2AddColumn() {
GCAddNewColumn(document.STSListControlWPQ2, “”);
}

function WPQ2ChangeColumn() {
GCEditDeleteColumn(document.STSListControlWPQ2, “”);
}
function WPQ2GridNewFolder() {
GCNewFolder(document.STSListControlWPQ2);
}

</script>

<script for=”STSListControlWPQ2″ event=”onresize” type=”text/javascript”>
GCOnResizeGridControl(document.STSListControlWPQ2);
</script>

<script for=”window” event=”onresize” type=”text/javascript”>
GCWindowResize(document.STSListControlWPQ2);
</script>

<script type=”text/javascript”>
function _spBodyOnLoad() {
GCWindowResize(document.STSListControlWPQ2);
GCActivateAndFocus(document.STSListControlWPQ2);
}
</script>

<script for=”document” event=”onreadystatechange” type=”text/javascript”>
if (document.readyState == “complete”) {
if (TestGCObject(document.STSListControlWPQ2)) {
GCActivateAndFocus(document.STSListControlWPQ2);
}
else {
GCNavigateToNonGridPage();
}
}

function GCNavigateToNonGridPage() {
//TODO: window.location.href = “”;
}
</script>

<script type=”text/javascript”>
var bGridViewPresent = true;
</script>

</asp:Content>

<asp:Content ID=”PageTitle” ContentPlaceHolderID=”PlaceHolderPageTitle” runat=”server”>
Datasheet view
</asp:Content>

<asp:Content ID=”PageTitleInTitleArea” ContentPlaceHolderID=”PlaceHolderPageTitleInTitleArea” runat=”server” >
<asp:LinkButton runat=”server” id=”linkList”></asp:LinkButton>
</asp:Content>

Add Below to Elements.xml file

<?xml version=”1.0″ encoding=”utf-8″?>
<Elements xmlns=”http://schemas.microsoft.com/sharepoint/”&gt;
<CustomAction Id=”DatasheetReviveButton”
RegistrationType=”ContentType”
RegistrationId=”0x01″
Location=”CommandUI.Ribbon.ListView”
Rights=”EditListItems”>
<CommandUIExtension>
<CommandUIDefinitions>
<CommandUIDefinition Location=”Ribbon.List.ViewFormat.Controls._children”>
<Button Id=”DatasheetReviveButton.OpenInDatasheetView”
Alt=”$Resources:core,cui_ButDataSheetView;”
Image16by16=”/_layouts/$Resources:core,Language;/images/formatmap16x16.png” Image16by16Top=”-48″ Image16by16Left=”-144″
Image32by32=”/_layouts/$Resources:core,Language;/images/formatmap32x32.png” Image32by32Top=”0″ Image32by32Left=”-288″
LabelText=”$Resources:core,cui_ButDataSheetView;”
ToolTipTitle=”$Resources:core,cui_ButDataSheetView;”
ToolTipDescription=”$Resources:core,cui_STT_ButDatasheetView;”
Sequence=”10″
Command=”DatasheetRevive_OpenInDatasheetView”
TemplateAlias=”o1″
/>
</CommandUIDefinition>
<CommandUIDefinition Location=”Ribbon.Library.ViewFormat.Controls._children”>
<Button Id=”DatasheetReviveButton.OpenInDatasheetView”
Alt=”$Resources:core,cui_ButDataSheetView;”
Image16by16=”/_layouts/$Resources:core,Language;/images/formatmap16x16.png” Image16by16Top=”-48″ Image16by16Left=”-144″
Image32by32=”/_layouts/$Resources:core,Language;/images/formatmap32x32.png” Image32by32Top=”0″ Image32by32Left=”-288″
LabelText=”$Resources:core,cui_ButDataSheetView;”
ToolTipTitle=”$Resources:core,cui_ButDataSheetView;”
ToolTipDescription=”$Resources:core,cui_STT_ButDatasheetView;”
Sequence=”10″
Command=”DatasheetRevive_OpenInDatasheetView”
TemplateAlias=”o1″
/>
</CommandUIDefinition>
</CommandUIDefinitions>
<CommandUIHandlers>
<CommandUIHandler Command=”DatasheetRevive_OpenInDatasheetView” CommandAction=”javascript: var url='{SiteUrl}/_layouts/15/EGIT.ECMDataSheetView/datasheet.aspx?List={ListId}&amp;View=’+ctx.view; window.location.href=url;” EnabledScript=”return typeof(window.ActiveXObject) == ‘function’; ” />
</CommandUIHandlers>
</CommandUIExtension>
</CustomAction>
</Elements>

/*end*/

Add this to feature and deploy as wsp. Activate the feature and there you go.

 

Advertisements

About jollsam

10 years of experience in SharePoint Development, Administration and Solution/Infrastructure Architecture.
This entry was posted in sharepoint. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s