Category: ECMA

Get current site web URL in JavaScript or JQuery

 

Use JavaScript variable name “L_Menu_BaseUrl” to get current web url and map it to any code in JavaScript.  This variable will be initialized on SharePoint pages by default. Below script will be always there when you do view source of the page:

var L_Menu_BaseUrl=”/ESearchCenter”;
var L_Menu_LCID=”1033″;
var L_Menu_SiteTheme=””;

OR

You can make use of  below variables

1. var webUrl = _spPageContextInfo.webServerRelativeUrl;
2. var siteUrl = _spPageContextInfo.siteServerRelativeUrl;

Check dupliacte in list SharePoint ECMA Script : Client object model

Screen Shots:-

 

 Solution:-

//Get the list item from the Announcements list whose Id is 4. Note that this is the ID of the item in the list, not a reference to its position in the collection.
var itemId = 1;
var targetListItem;
var collListItem;
function runCode(txtBoxValue) {
alert(txtBoxValue);
var clientContext = new SP.ClientContext();
var targetList = clientContext.get_web().get_lists().getByTitle(‘TestECMAList’);
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml(”+txtBoxValue+’10’);
collListItem = targetList.getItems(camlQuery);

//targetListItem = targetList.getItemById(itemId);
clientContext.load(collListItem);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded() {

//alert(‘Request succeeded. \n\nRetrieved Item is: ‘ + targetListItem.get_item(‘Title’));
var listItemInfo = ”;
var listItemEnumerator = collListItem.getEnumerator();

while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
listItemInfo += ‘\nID: ‘ + oListItem.get_id() +
‘\nTitle: ‘ + oListItem.get_item(‘Title’) ;
}
if(listItemInfo.toString()==””)
{
alert(‘Item not found’);
}
else
{
alert(listItemInfo.toString());
}

}
function onQueryFailed(sender, args) {
alert(‘Request failed. \nError: ‘ + args.get_message() + ‘\nStackTrace: ‘ + args.get_stackTrace());
}