SharePoint javascript and web services sample
SharePoint web services are sometimes very easy to use and I just created a small sample that uses some Javascript to delete a list by using a SharePoint web service. Darren has build this excellent Javascript API to talk to SharePoint web services and Office Live web services.
Before you can start you need to do a few things in SharePoint:
- Create a document library called zsLibrary;
- In the document library create a folder called spconnect;
- Upload the files from this download link to the folder spconnect;
- Create a custom list called Test.
Now you are ready to add a few lines of Javascript to a Content Editor web part.
First add a content editor web part to your site and add the following code to the content editor (Source editor):
<script type="text/javascript" src="zsLibrary/spconnect/SPAPI_Lists.js"></script>
<script type="text/javascript" src="zsLibrary/spconnect/SPAPI_Core.js"></script>
<script language="JavaScript">
function DeleteList(){
var lists = new SPAPI_Lists(L_Menu_BaseUrl)
var res = lists.deleteList('Test');
if (res.status == 200)
{
alert('The list was deleted.');
}
else
{
alert('Error: could not delete list Test');
}
}
</script>
<A HREF="javascript:DeleteList();">Delete list</A>
When you are done click on the Delete list and the Test list should be deleted (it needs a refresh). As you can see you can call the web service without the need to re-authenticate and that is very handy.
You can also see that I use the L_Menu_BaseUrl variable to get to the current site. This one is part of the global javascript variables in SharePoint, below I list the ones that I know:
L_Menu_BaseUrl - the base URL of the site / subsite. Very useful when you need to determine the absolute path of the site in JavaScript. Example: document.location = L_Menu_BaseUrl + 'lists/calendar/allitems.aspx' //redirects to Calendar list
L_Menu_LCID - the LCID of the site you're in. Useful if you need to determine the language of the site. The list of Locale IDs can be found here. I'm using the LCID for localizations in ERTE project. See the example of checking LCID below:
L_Menu_SiteTheme - the name of the theme applied to the site.
There is one more useful variable, but this one can't be used on custom master pages that you created. This one is used in the SharePoint's default pages:
_spUserId - the ID of the logged in user.
0 Comments |
Posted
Tuesday, 28 Oct 2008 08:26
by
Hans Blaauw
in
Programming