Search code examples
javascriptsharepoint-2010dopostbacksharepoint-clientobject

How to read a column type SPUser, DateTime, Currency with EcmaScript?


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.

Thanks.


Solution

  • Okay, I got a solution Sharepoint.Stackoverflow.com from user Vardhaman Deshpande. This works.

    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!');
    }