Monday, April 27, 2009

Call SharePoint WebService using jQuery

jQuery is very popular JavaScript library,it is light, fast and makes javascript work lot lot easier. I was using this library for
one of my ASP.Net project. Was just cusrious to know how this can be used to call SharePoint webservices. With jQuery you can call SharePoint webservice with couple of lines of code. Also jQuery makes XML utilization very easy. The sample code below shows how to traverse XML nodes which is returned by GetListItems method of Lists.asmx webservice. The XML returned by GetListItems method uses XML namespace, which cuases some problem while using jQuery. The technique used in sample below works fine on IE 6, i have not tried this on other browsers.

In order to use jQuery APIs inside ShaerPoint page, you need to add the script file reference to your page.
Following steps show you how to reference this library:
1. Copy the jQuery javascript file to 12\Tempalte\Layout\1033 folder
2. Edit your master page and add following script file reference
<script type="text/javascript" language="javascript" src="/_layouts/1033/jquery-1.3.2.min.js"></script>
Following sample code reads document name from sharepoint document library (Shared Documents):


<script type="text/javascript>
$(document).ready(function() {
var xmlData ="<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'><soap:Body><GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'><listName>Shared Documents</listName><query><Query
xmlns=''><OrderBy><FieldRef Name='Title' /></OrderBy></Query></query><viewFields><
ViewFields xmlns='' /></viewFields><queryOptions><QueryOptions xmlns='' /></queryOptions></GetListItems></soap:Body></soap:Envelope>";

$.ajax({
url: "/_vti_bin/lists.asmx",
type: "POST",
dataType: "xml",
data: xmlData,
complete:SuccessFunc,
error: ErrorFunc,
contentType: "text/xml; charset=\"utf-8\""
});

});

function SuccessFunc(result) {
//xml node with namespace need to be handled differently for jQuery
$(result.responseXML).find("z\\:row").each(function() {
alert($(this).attr("ows_BaseName"));
});
}

function ErrorFunc(result) {
alert(result.responseText);
}

</script>

Tuesday, April 7, 2009

Difference between SPWeb.Groups Vs SPWeb.SiteGroups

SPWeb has two sharepoint cross-site group collection, SPWeb.Groups and SPWeb.SiteGroups. SPWeb.Groups returns collection of cross-site groups which has some permission on the site. So if you add group from a site without any permission on the site, then this group wont appear in SPWeb.Groups collection, but it will appear in SPWeb.SiteGroups collection.
You can not use SPWeb.Groups.Add method to add new cross-site group, you need to use SPWeb.SiteGroup.Add method for this purpose.

Sunday, March 29, 2009

SharePoint Developer tools

If you are a serious SharePoint developer then you need to have set of tools which will assist you in increasing your productivity. SharePoint development needs understanding of various concepts like Features, Solutions, templates and site definitions etc. along with ASP.Net and WSS/MOSS SDKs.
While working on various SharePoint projects i used several tools, these tools helped me lot in debugging and also reduced my coding time. I have listed set of tools that i used below:
CAML Query builder
I think this is one tool that all SharePoint developer will have. This tool helps you to build CAML queries, very handy tool for checking various CAML query elements
Download Location: http://www.u2u.be/res/Tools/CamlQueryBuilder.aspx


Visual Studio Extensions for WSS 3
Tools for developing custom SharePoint applications: Visual Studio project templates for Web Parts, site definitions, and list definitions; and a stand-alone utility program, the SharePoint Solution Generator. This tool reduced lot of my packaging time. New VSeWSS 1.3 provides lot more features and also has support for x64.
Download Location (VSeWSS 1.3 for VS 2008): http://www.microsoft.com/downloads/details.aspx?FamilyID=B2C0B628-5CAB-48C1-8CAE-C34C1CCBDC0A&displaylang=en

Download Location (VSeWSS 1.5 for VS 2005, No support for x64):http://www.microsoft.com/downloads/details.aspx?familyid=7BF65B28-06E2-4E87-9BAD-086E32185E68&displaylang=en


Reflector
No introduction required for this tool, very popular tool. .NET Reflector enables you to easily view, navigate, and search through, the class hierarchies of .NET assemblies. Use this tool to understand implementation of various SharePoint SDKs.
Download Location: http://www.red-gate.com/products/reflector/

Fiddler:
This tool logs all http requests between client and web server. This saved lot of my time while resolving issues related to generating SSRS reports using SharePoint webservices. If you are consuming Webservice then this tool will be of great help.
Download Location: http://www.fiddler2.com/fiddler2/version.asp


IE Developer toolbar
This is an add-on to IE 6 and 7, helps in debugging webpages. This is of great help when you are branding SharePoint site or developing custom webpart or controls. Download Location: http://www.microsoft.com/downloads/details.aspx?familyid=e59c3964-672d-4511-bb3e-2d5e1db91038&displaylang=en


MOSS Search Query Tool
Tool to run MOSS search queries (Keyword or SQL)
Download Location: http://www.wssdemo.com/Lists/Resources/DispForm.aspx?ID=893


WSP Builder
A SharePoint Solution Package (WSP) creation tool for WSS 3.0 & MOSS 2007
Download Location: http://www.codeplex.com/wspbuilder

Wednesday, February 25, 2009

SharePoint Document library size

SharePoint document library UI doesn't show the library size. SharePoint designer
provides interface where you can view the document library size.
To view doc library size:
1) Open site from SharePoint designer
2) Right click on document library tree node, and select
properties menu item.


This will open the properties window with document library size.

You can also read the size programatically. StorageManagementInformation method of SPSite object can be used to read the library/list/document size of entire site collection.
Following code shows how to read document library size:


using (SPSite site = new SPSite("http://servername"))
{
DataTable tbl;
tbl = site.StorageManagementInformation(
SPSite.StorageManagementInformationType.DocumentLibrary,
SPSite.StorageManagementSortOrder.Decreasing,
SPSite.StorageManagementSortedOn.Size, 100);
foreach (DataRow row in tbl.Rows)
{
foreach (DataColumn column in tbl.Columns)
MessageBox.Show("Doc Lib Name :" + row["Title"].ToString() + " Size (bytes): " + row["Size"].ToString());
}
}

Tuesday, January 20, 2009

Set Default date and time of DateTime field

The built-in DateTime column allows you to set default date to current date. But this doesn't give an option to set time. By default time is set to 12:00AM.
Default time can be set to required time easily using calculated value.
In the default value option select the "Calculated Value" and set the formula as :
=Today+"09:00 AM"
This will set the default date to current date and time to 9:00AM.
Following diagram shows screen to configure this:

Sunday, December 28, 2008

Assigning permission programatically

Windows SharePoint Services manages permissions through role definations. SPRoleDefinition and SPRoleDefinition classes provides methods to assign users to roles. Following code snippet shows how to assign the Contributor role to a custom SharePoint group programatically:

using (SPSite site = new SPSite(siteCollection))
{
using (SPWeb subWeb = site.OpenWeb(siteName))
{
//You need to break role inheritence if you want to assign unique permission to subsite
if (!subWeb.HasUniqueRoleAssignments)
subWeb.BreakRoleInheritance(true);
SPRoleDefinition roleDefination = parentWeb.RoleDefinitions["Contrubute"];
SPRoleAssignment roleAssignment = new SPRoleAssignment("MyCustomerGroup");
roleAssignment.RoleDefinitionBindings.Add(roleDefination);
subWeb.RoleAssignments.Add(roleAssignment);
subWeb.Update();
}
}


Users or Groups can be assigned permission to List or Document library or list item. Following code shows assigning permission on Folder of a document library for a custom sharepoint group:


using (SPSite site = new SPSite(siteCollection))
{
using (SPWeb web = site.OpenWeb(siteName))
{
SPDocumentLibrary docLib = (SPDocumentLibrary)web.Lists[libraryName];
//Get folder
SPListItem item = docLib.Folders[1];
if (!item.HasUniqueRoleAssignments)
item.BreakRoleInheritance(true);
SPRoleAssignment roleAssignment = new SPRoleAssignment(group);
SPRoleDefinition roleDefination = web.RoleDefinitions["Contribute"];
roleAssignment.RoleDefinitionBindings.Add(roleDefination);
item.RoleAssignments.Add(roleAssignment);
item.Update();
}
}

Monday, December 8, 2008

SharePoint Alert customization

Recently i was working on customizing SharePoint alerts. Sharing different things i learnt while working on this:
You can create one or more alerts for a list or document library in a SharePoint site. When creating alerts you have option of what event and when to get notified. Alerts can be managed by Administrators or Users. SharePoint object model provides methods to manage Alerts programmatically.

SPAlert class provides details on alert like alert frequency, alert type,user who created the alert. This class also provides a method to update alert details. Following code snippet shows how to create SharePoint Alert programmatically:

using (SPSite site = new SPSite("http://servername"))
{
using (SPWeb web = site.OpenWeb())
{
SPUser user = web.SiteUsers["domain name\\username];
SPAlert newAlert = user.Alerts.Add();
newAlert.AlertType = SPAlertType.List;
newAlert.List = web.Lists["List Name"];
newAlert.EventType = SPEventType.All;
newAlert.AlertFrequency = SPAlertFrequency.Immediate;
//passing true to Update method will send alert confirmation mail
newAlert.Update(false);
}
}

SharePoint stores the alert information like web application URL and List URL in content database. Because of this, the links inside alert mail will break if web application URL or List URL is changed. To fix this user need to update the alert manually from alert manager or Administrator can run a script to update alerts. Refer this link for more details on this issue.

Alert mails can be customized in two ways:
Approach 1
You can customize the Alert tempaltes of WSS. Alert tempaltes are located in
"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\Template\XML\AlertTemplates.xml"
Following link has lot of details on this approach:
http://blogs.msdn.com/sharepointdeveloperdocs/archive/2007/12/07/customizing-alert-notifications-and-alert-templates-in-windows-sharepoint-services-3-0.aspx

Approach 2
You can also customize alert mail format by implementing IAlertNotifyHandler interface. Following Microsoft KB article has details on this:
http://support.microsoft.com/kb/948321/en-us

How to debug custom alert handlers
Alerts are generated by WSS timer job. Time job reads the event log and sends the alert. So to debug custom alert handlers you need to attach timer process to debugger (OWSTIMER.exe).

Though we can customize alert mail format, i dint find a way to customize the alert confirmation mail format.