Solved

Unable to get items from client side javascript

Posted on 2014-04-18
6
2,261 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 31

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 31

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 31

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

We had a requirement to extract data from a SharePoint 2010 Customer List into a CSV file and then place the CSV file into a directory on the network so that the file could be consumed by an AS400 system. I will share in Part 1 how to Extract the Da…
OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now