共用方式為


Getting Sharepoint List Id using javascript and list.asmx.

Many times, we may need list id of sharepoint list in javascript, for example we may need to create a link for list new item form. One way is to find the list id manually and hard code in the link. If this solution need to be deployed in other environment then we will have to modify the hard coded list id. Other option is to read the list id dynamically and update the link.

One of possible ways to get list id is using list.asmx. Below is the sample code to read list id using javascript and list.asmx.In the below code , we are calling List.asmx on the sitecollection. On the success of call we get xml with information about the list. After that we find the Id of list from xml and update the href link or use id as per requirement.

 function GetListID() {
 
 /* url of site collection + /_vti_bin/Lists.asmx */
 wsURL = "https://..../_vti_bin/Lists.asmx"
 
 var soapEnv = "<soap:Envelope xmlns:xsi='https://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='https://www.w3.org/2001/XMLSchema' xmlns:soap='https://schemas.xmlsoap.org/soap/envelope/'> <soap:Body> <GetList xmlns='https://schemas.microsoft.com/sharepoint/soap/'> <listName>ListNameWhoseIdIsNeeded</listName></GetList></soap:Body></soap:Envelope>";
 
 
 $.ajax({
 url: wsURL,
 type: "POST",
 dataType: "xml",
 data: soapEnv,
 complete: callComplete,
 contentType: "text/xml; charset=\"utf-8\""
 });
}
 function callComplete(xData, status) {
 var id
 $(xData.responseText).find("List").each(function () {
 id = $(this).attr('ID');
 });
 
 /* Now you have list id in id variable...
 all we need to do is update the 
 href of link with this id */
 
 }