Solved

Unable to get items from client side javascript

Posted on 2014-04-18
6
2,381 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 32

Accepted Solution

by:
Jamie McAllister MVP earned 500 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Check if field exists SPUtility 5 48
Multiple of Image Swaps 5 41
Slow Down an Animation 3 29
Sticking text to shapes in Raphael.js 6 41
These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
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…

738 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