I have a list in SharePoint 2010 with some columns. All are default types. So I have

  • "Single line of text"
  • "Multiple line of text"
  • "Date and Time"
  • "Choice"
  • "Number"
  • "Currency"
  • "Person or Group"

My aim is to have a custom ribbon tab or group where I can perform some action on this list. As a starting point I created an Empty Element in my Visual Studio solution and put inside Elements.xml my buttons. This works so far. I also figured out how to do a postback to react on pressed button. This postback refers to a JavaScript file.

Before performing some action I tried first to read the given contents and return them using alert('first field: ' + field1). In first called function I have

function calledPostbackFunction(string button) {  
    var context = SP.ClientContext.get_current();  
    this.site = context.get_site();  
    this.web = context.get_web();  
    context.load(this.site);  
    context.load(this.web);  
    context.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceded(button), Function.createDelegate(this, this.onQueryFailed));

How can I get the content from listed column types? I remember that I was able to read single text line and choice, but the rest crashed. So I guess I have to convert it any way. But how? IntelliSense doesn't helps a lot.

SUBQUESTION: I would skip using EcmaScript if you can tell me how to doPostBack to a .cs file where I can use Client Object Model. I found something but didn't work/ understand.

Yes, I though this will be easy, but it was not. At least because I only know C# a bit, no EcmaScript.

This questions comes from stackoverflow.

link|improve this question
feedback

2 Answers

up vote 3 down vote accepted

Here is How to get the value of each type of field:

Title – SP.ListItem.get_item(‘Title‘);

ID – SP.ListItem.get_id();

Url -SP.ListItem.get_item(‘urlfieldname‘).get_url()

Description – SP.ListItem.get_item(‘descriptionfieldname‘).get_description();

Current Version – SP.ListItem.get_item(“_UIVersionString“);

Lookup field – SP.ListItem.get_item(‘LookupFieldName’).get_lookupValue();

Choice Field – SP.ListItem.get_item(‘ChoiceFieldName‘);

Created Date – SP.ListItem.get_item(“Created“);

Modified Date – SP.ListItem.get_item(“Modified“); -> case sensitive does not work with ‘modified’

Created By – SP.ListItem.get_item(“Author“).get_lookupValue());

Modified by – SP.ListItem.get_item(“Editor“).get_lookupValue());

File  – SP.ListItem.get_file();

File Versions -  File.get_versions();.

Content Type – SP.ListItem.get_contentType();

Parent List – SP.ListItem.get_parentList();

from: http://www.learningsharepoint.com/2011/07/06/how-to-get-various-item-fields-using-client-object-model-ecmascript-sharepoint-2010/

UPDATE: The following code is working and tested.

var item;
function getItemById(itemId){

    var clientContext = new SP.ClientContext.get_current();

    var web = clientContext.get_web();

    var list = web.get_lists().getByTitle('myList');

    item = list.getItemById(itemId);

    clientContext.load(item);

    clientContext.executeQueryAsync(onSuccess, onFailure);
}
function onSuccess(){

    alert(item.get_item("My User column").get_lookupValue());
}
function onFailure(){

    alert('Failure!');
}
link|improve this answer
Thank you very much, this looks like what I searched for. I suggest that I can use var userInColumn = SP.ListItem.get_item("My User column").get_lookupValue() to get a valid SPUser object. I will test it. – Shegit Brahm Jan 25 at 11:55
Okay, from scratch I can't use it like this. That means, atm is Microsoft JScript runtime error: Object doesn't support property or method 'get_item' my error. I also have to figure out, which *.js I have to reference, because IntelliSense does not show SP.ListItem. – Shegit Brahm Jan 25 at 16:43
I have updated my main answer. Have a look. – Vardhaman Deshpande Jan 25 at 18:12
Thank you very much for your added code. As far as I assume, it looks correct. It is just that I start to get stupid errors like no property as executeQueryASync. Debugger goes through, no error until this line. – Shegit Brahm Jan 26 at 10:55
Oh man. My bad. Its executeQueryAsync and not executeQueryASync. Th "s" in "Async" is lower case. I have updated the original code again. – Vardhaman Deshpande Jan 26 at 11:24
show 1 more comment
feedback

If I understand correctly, you want to add buttons in the ribon that will perform some action on items in a list, using Javascript.

First, you do not need C# code, you can put your javascript directly inside your CustomAction element, like so:

<CustomAction
Id="ActionName"
Title="My Action"
Rights="EditListItems"
Description="This is my action"
Sequence="301"
Location="CommandUI.Ribbon.ListView">
<CommandUIExtension>
  <CommandUIDefinitions>
    <CommandUIDefinition Location="Ribbon.ListItem.Actions.Controls._children">
      <Button
                 Id="Ribbon.ListForm.Display.Manage.MyAction"
                 Alt="Do some action"
                 Image32by32="/_layouts/1036/images/formatmap32x32.png"
                 Image32by32Left="-96"
                 Image32by32Top="0"
                 LabelText="Do some action"
                 Command="MyActionCommand"
                 TemplateAlias="o1"
                 Sequence="301"/>
    </CommandUIDefinition>
  </CommandUIDefinitions>
  <CommandUIHandlers>
    <CommandUIHandler
                Command="MyActionCommand"
                CommandAction="javascript:MyActionFunction({SelectedItemId});"
                EnabledScript="javascript:CustomSingleEnable();" />
  </CommandUIHandlers>
</CommandUIExtension>

Note that in the example I am simply calling a function that is in a separate javascript file already included (through the master page, for example).

Next, you can load your item from that javascript function (using the selected item ID passed as argument):

function MyActionFunction(selectedItemIdArg) {
var clientContext = new SP.ClientContext.get_current();

if (clientContext != undefined && clientContext != null) {
    this.currentSite = clientContext.get_site();
    this.targetList = clientContext.get_web().get_lists().getByTitle('My List');
    this.currentItem = targetList.getItemById(selectedItemIdArg);
    clientContext.load(this.targetList);
    clientContext.load(this.currentSite, 'Url');
    clientContext.load(this.currentItem);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

}

Your onQuerySucceeded will then be populated with values from your selected item:

function onQuerySucceeded() {
var url = this.currentSite.get_url();
// Do somethign with this.currentItem

}

If you want to act on multiple items, you will need to modify this slightly to use something like:

var ctx = SP.ClientContext.get_current();
var items = SP.ListOperation.Selection.getSelectedItems(ctx);

To get each field values, you can use the information provided by Vardhaman.

link|improve this answer
Ah if you want to handle multiple selected items you'll also want to remove the line EnabledScript="javascript:CustomSingleEnable();" in your CustomAction definition. – Louis Jan 24 at 21:25
Thank you very much for your answer. It steps into the way how to handle it at all, but that I already figured out in general. My problem arised as I wanted to get some values of different columns and I was not able to handle it. In C# I know how to use Convert.ToXXX(item["My Column"].ToString()); – Shegit Brahm Jan 25 at 12:00
feedback

Your Answer

 
or
required, but never shown

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