Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Unable to get items from client side javascript

Posted on 2014-04-18
6
Medium Priority
?
2,537 Views
Last Modified: 2014-04-24
I have this function that I'm calling from a ribbon button, but it's not returning any items. If i try to load a specific property, I get a property has not been initialized error. For example, I'm trying to display an alert with the description property.

function CopyLinkToClipboard() {
    // First get the context and web
    var ctx = SP.ClientContext.get_current();
    this.web = ctx.get_web();
    // Get the current selected list, then load the list using the getById method of Web (SPWeb)
    var listId = SP.ListOperation.Selection.getSelectedList();
    var sdlist = this.web.get_lists().getById(listId);
    // Get the currently selected item of the list. This will return a dictionary with an id field
    var items = SP.ListOperation.Selection.getSelectedItems(ctx);
    var mijnid = items[0];
    // Request the list item from the server using the getItemById method. This will load all properties.   
    // If needed, one could pre-request the fields to be loaded to preserve bandwidth.
    this.listItem = sdlist.getItemById(mijnid.id);
    // load the item in the context for batch operation.
	
    ctx.load(this.listItem);
    //Execute the actual script on the server side. Specify delegates to handle the response. 
    ctx.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

// Delegate that is called when server operation is complete upon success.
function onQuerySucceeded(sender, args) {
    // Request url by using the get_item method. It will return the Url field type, which has a Url property. 
    //var url = this.listItem.get_item('_dlc_DocIdUrl').get_url();
	var url = this.listItem.get_item('_dlc_DocIdUrl').get_url();
    // Request the name of the document. 
    var title = this.listItem.get_item('_dlc_DocId');
	description = this.listItem.get_item('Description');
	alert(description);
    // Copy Link To Clipboard (Only works in Internet Explorer)
    window.clipboardData.setData('Text', url);
    // Notify the user that the link was successfully copied
    var notificationId = SP.UI.Notify.addNotification('Link Successfully Copied to Clipboard');
}

// Delegate that is called when server operation is completed with errors.
function onQueryFailed(sender, args) {
    alert('failed ' + args.toString());
}

Open in new window

0
Comment
Question by:brendanlefavre
  • 3
  • 3
6 Comments
 
LVL 32

Expert Comment

by:Jamie McAllister MVP
ID: 40011212
I do this a little bit differently. I get the web and list id from the ribbon action itself;

-<CommandUIHandlers>

<CommandUIHandler Command="MyProj.SP.GSD.GetCmd" CommandAction="javascript:CustomCommandAction('{SiteUrl}', '{ListId}');" EnabledScript="javascript: SP.ListOperation.Selection.getSelectedItems().length > 0;"/>

</CommandUIHandlers>

Open in new window


Then my script has that information as a parameter, it's all just about the selected item. You might be getting the wrong id from your calls to list id etc.

function CustomCommandAction(siteurl, listid) {
    
    resultsString = '';
    this.currentSiteUrl = siteurl;
    
    var ctx = SP.ClientContext.get_current();// get client context
    this.spContext = ctx;
    
    var web = ctx.get_web();
    var selectedItems = SP.ListOperation.Selection.getSelectedItems(ctx); // Get selected List items

    for (var item in selectedItems) {
        if (selectedItems[item].fsObjType == 0) {
            resultsString += selectedItems[item].id + '-';   
            }
        }

    this.listId = listid;
    
    this.listname = '';

    try {
        var sdlist = web.get_lists().getById(listid);
        this.listname = sdlist.get_title();
    }
    catch (err) {
        //Handle errors here
    }
    
    this.spContext.load(this.spContext.get_site());// To fetch site properties 
    
    this.spContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

Open in new window

0
 

Author Comment

by:brendanlefavre
ID: 40011423
When testing out the solution that you have provided, the button is remaining disabled, even though an item has been selected.

Get Action
Cheers,
Brendan
0
 

Author Comment

by:brendanlefavre
ID: 40011488
I have resolved the issue with the enable/disable of the button, needed to update the command name reference.

for the onQuerySucceeded, how do I handle loading the list item properties?
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 32

Accepted Solution

by:
Jamie McAllister MVP earned 2000 total points
ID: 40011606
Hi, the method is below. Notice I'd stored my list id in a global variable set from the Ribbon action value...

function onQuerySucceeded(sender, args) {

    var options = SP.UI.$create_DialogOptions();
    var siteguid = spContext.get_site().get_id(); // get current site guid
    var sitecolurl = spContext.get_site().get_url();
    var siteurl = this.currentSiteUrl;
    var listguid = this.listId;
    var listname = this.listname;
    var appUrl;
    appUrl = sitecolurl + "/ProcessManager/DocumentInitialization.aspx?listguid=" + listguid + "&listname=" + listname + "&siteurl=" + siteurl + "&siteguid=" + siteguid + "&items=" + resultsString;
    
    options.url = appUrl;
    options.height = 600;
    options.width = 800;
    options.dialogReturnValueCallback = Function.createDelegate(null, DialogClosedCallback);
    SP.UI.ModalDialog.showModalDialog(options); // To Open Page in Model Dialogue
}

Open in new window

0
 

Author Comment

by:brendanlefavre
ID: 40011675
I have applied the latest suggestion, and I'm getting an error when the DialogClosedCallback function is being called.

function CustomCommandAction(siteurl, listid) {

    resultsString = '';
    this.currentSiteUrl = siteurl;

    var ctx = SP.ClientContext.get_current();// get client context
    this.spContext = ctx;

    var web = ctx.get_web();
    var selectedItems = SP.ListOperation.Selection.getSelectedItems(ctx); // Get selected List items

    for (var item in selectedItems) {
        if (selectedItems[item].fsObjType == 0) {
            resultsString += selectedItems[item].id + '-';
        }
    }

    this.listId = listid;

    this.listname = '';

    try {
        var sdlist = web.get_lists().getById(listid);
        this.listname = sdlist.get_title();
    }
    catch (err) {
        //Handle errors here
    }

    this.spContext.load(this.spContext.get_site());// To fetch site properties 

    this.spContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded(sender, args) {

    var options = SP.UI.$create_DialogOptions();
    var siteguid = spContext.get_site().get_id(); // get current site guid
    var sitecolurl = spContext.get_site().get_url();
    var siteurl = this.currentSiteUrl;
    var listguid = this.listId;
    var listname = this.listname;
    var appUrl;
    appUrl = sitecolurl + "/ProcessManager/DocumentInitialization.aspx?listguid=" + listguid + "&listname=" + listname + "&siteurl=" + siteurl + "&siteguid=" + siteguid + "&items=" + resultsString;

    options.url = appUrl;
    options.height = 600;
    options.width = 800;
    options.dialogReturnValueCallback = Function.createDelegate(null, DialogClosedCallback);
    SP.UI.ModalDialog.showModalDialog(options); // To Open Page in Model Dialogue
}

Open in new window

0
 
LVL 32

Expert Comment

by:Jamie McAllister MVP
ID: 40011765
That's because you need to have that defined;

function DialogClosedCallback() { SP.UI.ModalDialog.RefreshPage(SP.UI.DialogResult.OK); }

Open in new window

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question