brendanlefavre
asked on
Unable to get items from client side javascript
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());
}
ASKER
ASKER
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?
for the onQuerySucceeded, how do I handle loading the list item properties?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
}
That's because you need to have that defined;
function DialogClosedCallback() { SP.UI.ModalDialog.RefreshPage(SP.UI.DialogResult.OK); }
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.
Open in new window