Wednesday, July 23, 2008

How access SharePoint (MOSS) User Profile Web Service using JavaScript in SharePoint (MOSS) Content Editor Web Part:

How access SharePoint (MOSS) User Profile Web Service using JavaScript in SharePoint (MOSS) Content Editor Web Part:
I tried this when I had the requirements in which we need to pass the user profile information let’s say Account Name (User NTID), First Name and Last Name to the some other function in JAVA SCRIPT. For accessing current logged in user information you need to call GetUserProfileByName function of userprofileservice web service.
I have written a JavaScript code for accessing the GetUserProfileByName function of userprofileservice web service:

<script type="text/javascript" language="javascript">

//Complete URL of the page in which you write this code
// http://amitkumarmca04.blogspot.com/sites/MYSITE/AMIT.aspx
var siteCompleteURL=document.URL;

//Split the complete URL from ‘//’
// componentList[0]=’http:’
// componentList[1]= ‘amitkumarmca04.blogspot.com/sites/MYSITE/AMIT.aspx’
componentList=siteCompleteURL.split('//');

//Split the URL (without //) with ‘/’
// siteURL[0]= ‘amitkumarmca04.blogspot.com’
// siteURL[1]=’ sites’
// siteURL[2]=’ MYSITE’
// siteURL[3]=’ AMIT.aspx’
siteURL=componentList[1].split('/');
var baseURL = componentList[0] + "//" + siteURL[0] + "/";
// baseURL=’http:’ + "//" + ‘amitkumarmca04.blogspot.com’ + "/";



var siteURL = componentList[0] + "//" + siteURL[0] + "/" + "sites/ MYSITE /";
// siteURL =’http:’ + "//" + ‘amitkumarmca04.blogspot.com’ + "/" + ‘sites/MYSITE/’

var strAccountName="";
var strFirstName=""
var strLastName=""
var str = '';

function GetRootUrl()
{
return siteURL;
}


function fetchUserData()
{
var a = new ActiveXObject("Microsoft.XMLHTTP");

if(a == null) return null;

a.Open("POST", GetRootUrl() + _vti_bin/userprofileservice.asmx", false);
a.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
a.setRequestHeader("SOAPAction", " http://microsoft.com/webservices/SharePointPortalServer/UserProfileService/GetUserProfileByName");


var d =
"<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:tns=\"http://schemas.microsoft.com/sharepoint/soap/\"><soap:Body>< GetUserProfileByName xmlns=\"http://microsoft.com/webservices/SharePointPortalServer/UserProfileService\"> <AccountName></AccountName></ GetUserProfileByName></soap:Body></soap:Envelope>";

a.Send(d);

var xmlDoc=a.responseXML;

for(i=0;i<4;i++)
{
if(xmlDoc.getElementsByTagName("Name")[i].childNodes[0].nodeValue=='AccountName')
{
strAccountName=xmlDoc.getElementsByTagName("Value")[i].childNodes[0].nodeValue;
}
else if(xmlDoc.getElementsByTagName("Name")[i].childNodes[0].nodeValue=='FirstName')
{
strFirstName=xmlDoc.getElementsByTagName("Value")[i].childNodes[0].nodeValue;
}
else if(xmlDoc.getElementsByTagName("Name")[i].childNodes[0].nodeValue=='LastName')
{
strLastName=xmlDoc.getElementsByTagName("Value")[i].childNodes[0].nodeValue;
}

}


}

fetchUserData ();
document.write(“Account Name=” + strAccountName);
document.write(“First Name=” + strFirstName);
document.write(“Last Name=” + strLastName);
</script>
In the soap of this web service, when you are not passing any value for the tag <AccountName></AccountName>, then it will return you to the details of current logged in user, if you pass any value between this tag then it will return you to the details of the user for which you have passed the value. This value for this tag would be the NTID of the user e.g. MYCOMPUTER\AMITKUMAR.

Happy Coding :).

Thursday, July 03, 2008

How access SharePoint (MOSS) List data using JavaScript in SharePoint (MOSS):


How access SharePoint (MOSS) List data using JavaScript in SharePoint (MOSS):

In the SharePoint (MOSS) some time you faced the requirements in which by using native SharePoint (MOSS) functionality you need to provide the best solution to the client. Suppose one of the example of this type of requirement is: Without using any server side code you need to display the data from the SharePoint (MOSS) list in the master page. In the SharePoint (MOSS) master page you will be not able to add any web part ( for example SharePoint List View Web Part). In this scenario you will be not able to write any code in the Microsoft Language (e.g. C#.net). At that type of scenario only one option left for the programmer i.e. client side code/client side script i.e. JAVA SCRIPT. Now, our motive is to write the script using JavaScript to fetch/access data from the SharePoint (MOSS) List. In this solution we used the SharePoint (MOSS) List web service to access data from SharePoint (MOSS) List (Custom list/Link List/Document Library/….).

For example I have one SharePoint (MOSS) List called RequestToDate with column name ‘Title’ and my requirement is to display data from the Title column. The JavaScript Code for this are given below:

<script type="text/javascript" language="javascript">

//Complete URL of the page in which you write this code
// http://amitkumarmca04.blogspot.com/sites/MYSITE/AMIT.aspx
var siteCompleteURL=document.URL;

//Split the complete URL from ‘//’
// componentList[0]=’http:’
// componentList[1]= ‘amitkumarmca04.blogspot.com/sites/MYSITE/AMIT.aspx’
componentList=siteCompleteURL.split('//');

//Split the URL (without //) with ‘/’
// siteURL[0]= ‘amitkumarmca04.blogspot.com’
// siteURL[1]=’ sites’
// siteURL[2]=’ MYSITE’
// siteURL[3]=’ AMIT.aspx’
siteURL=componentList[1].split('/');


var slideImagesArr=new Array();

var slideImagesDisplayArr=new Array();

var slideImageLinksArr=new Array();

var baseURL = componentList[0] + "//" + siteURL[0] + "/";
// baseURL=’http:’ + "//" + ‘amitkumarmca04.blogspot.com’ + "/";

var baseLibURL = componentList[0] + "//" + siteURL[0] + "/" + "sites/ MYSITE /Lists/RequestsToDate/DispForm.aspx?ID=";
// baseLibURL =’http:’ + "//" + ‘amitkumarmca04.blogspot.com’ + "/" + ‘sites/MYSITE/Lists/RequestsToDate/DispForm.aspx?ID='


var siteURL = componentList[0] + "//" + siteURL[0] + "/" + "sites/ MYSITE /";
// siteURL =’http:’ + "//" + ‘amitkumarmca04.blogspot.com’ + "/" + ‘sites/MYSITE/’

//GUID of RequestToDate list
var listGUID = "6f9d3029-120e-4177-9f9b-bdeae0598df2";

var str = '';

function GetRootUrl()
{
return siteURL;
}

function getNodeValue(obj,tag)
{
return obj.getElementsByTagName(tag)[0].firstChild.nodeValue;
}

function QueryListEx()
{
var a = new ActiveXObject("Microsoft.XMLHTTP");

if(a == null) return null;

a.Open("POST", GetRootUrl() + "_vti_bin/Lists.asmx", false);
a.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
a.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/GetListItems");


var d =
"<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:tns=\"http://schemas.microsoft.com/sharepoint/soap/\"><soap:Body><tns:GetListItems><tns:listName>{"+listGUID+"}</tns:listName><viewFields><ViewFields><FieldRef Name=\"ID\"/></ViewFields></viewFields></tns:GetListItems></soap:Body></soap:Envelope>";

a.Send(d);

var xmlDoc=a.responseXML;
var responseElement=xmlDoc.getElementsByTagName("z:row");
var temp = '';

for (i=0;i<responseElement.length;i++)
{
//Store Title Column Value
slideImagesArr[i] = responseElement[i].getAttribute('ows_Title');

//Store ID Column Value
slideImageLinksArr[i] = responseElement[i].getAttribute('ows_ID');

//Store Created date Column Value
slideImagesDisplayArr[i] = responseElement[i].getAttribute('ows_Created');
}


if (a.status != 200)
return null;
else
{
if (extractRows)
return a.responseXML.selectNodes('//Row');
else
return a.responseXML;
}
}

QueryListEx(false);



</script>

<DIV id=divMaster></DIV>
<script>
<!--



for (var x = 0; x <= slideImagesArr.length; x++)
{
if(slideImageLinksArr[x] > 0)
{
//Store Title Column Value in variable str
str = slideImagesArr[x];

}
}


document.getElementById("divMaster").innerHTML = str;

//-->
</script>






In the above code divMaster is the ID of the DIV, in which we are displaying the data from the SharePoint (MOSS) List called RequestToDate.

I think the above solution helps you in writing the code when you are not able to write any server side code (for example C#/C-Sharp) for fetching/ retrieving data from the SharePoint (MOSS) List.