2

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://schemas.microsoft.com/sharepoint/">
  <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://schemas.microsoft.com/sharepoint/">
  <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?

1

Instead of itemId use {SelectedItemId}

For more information see this

http://msdn.microsoft.com/en-us/library/office/ff458385(v=office.15).aspx

  • Thank you so much! This solved the problem. Now I can finally go on and make my C# doing what it should do. – Marco Ploerer Dec 9 '14 at 9:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.