mardi 9 décembre 2014

Ribbon Button in List, which sends Data to C# code



First, I'm not really a developer, but since I'm the only "sharepoint-guy" in my company I need to learn this, too. So here's my problem: I need a ribbon button, which sends data of the marked item to a C# application. This works perfectly fine with the context menu, but gives me a script error on the ribbon.


This is my working code for the context menu:



<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://ift.tt/sQmbje">
<CustomAction Id="SendTest"
Location="EditControlBlock"
Title="Sent Test"
RegistrationType="List"
RegistrationId="100"
ImageUrl="/_layouts/IMAGES/DOCLINK.GIF">
<UrlAction Url="javascript:__doPostBack('SendTest',{ItemId});"/>
</CustomAction>
</Elements>


This is the erroneus code for the ribbon:



<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://ift.tt/sQmbje">
<Control
ControlAssembly="SPTest.CustomMenuItem, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8b4e18645f42d2fd"
ControlClass="SPTest.CustomMenuItem.CustomItemAction" Sequence="50" Id="AdditionalPageHead"/>
<CustomAction
Id="SendToWCF"
RegistrationType="List"
RegistrationId="100"
Location="CommandUI.Ribbon.ListView"
Rights="EditListItems">
<CommandUIExtension>
<CommandUIDefinitions>
<CommandUIDefinition
Location="Ribbon.ListItem.Actions.Controls._children">
<Button
Id="SendToWCF.SendButton"
Alt="Send To WCF"
Sequence="1"
Image32by32="/_layouts/Images/search32x32.png"
Command="SendToWCFCommand"
LabelText="Send to WCF"
TemplateAlias="o1"
CommandType="General"/>
</CommandUIDefinition>
</CommandUIDefinitions>
<CommandUIHandlers>
<CommandUIHandler
Command="SendToWCFCommand"
CommandAction="javascript:__doPostBack('SendToWCF',{ItemId});" />
</CommandUIHandlers>
</CommandUIExtension>
</CustomAction>
</Elements>


This is my c# (for testing) - I replace the SendTest with SendtoWCF for the ribbon:



using System.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.ServiceModel;

namespace SPTest.CustomMenuItem
{
public class CustomItemAction : SPLinkButton
{
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);

if (this.Page.Request["__EVENTTARGET"] == "SendTest")
{
int itemId = Convert.ToInt32(this.Page.Request["__EVENTARGUMENT"]);

System.IO.TextWriter writer = new StreamWriter(@"C:\\tmp\\custommenuoutput.txt", true);

writer.WriteLine("Event Fired at:" + DateTime.Now.ToLongTimeString() + ": Item ID:" + itemId.ToString());

writer.Close();
}

}
}
}


When I use the context menu it works fine. this.Page.Request["__EVENTTARGET"] gets the correct value and the file is written to C:\tmp


When I use the ribbon I get a javascript error and __EVENTTARGET is still null:



Unhandled exception at line 359, column 21 in http://spdev/_layouts/15/sp.ribbon.debug.js?rev=lltVSagCIwhL+jnBu7MRag==

0x800a03eb - JavaScript runtime error: Expected ':'


Am I missing something. Do I have to tell him somehow, which Item he should use? Because at the moment it doesn't make a difference if none, one or more items are selected. Can this be the problem?








0 commentaires:

Enregistrer un commentaire