Monday, February 28, 2011

File Not Found exception on SharePoint 2010

There is a common practice of SharePoint Development is to create a windows or console application to fetch several SharePoint data. This method is widely used because there is no deployment steps to perform. The only limitation of using such application is, you have be on the SharePoint Server.


While trying to access to list information of a SharePoint 2010 site, I used a windows application to fetch data. To my surprise, I am getting a "File not found" exception. But the same code working perfectly fine in MOSS 2007


It's a very wired message, as I am not trying any file operation. And the message forced me to searched on the web several hours. And finally understood that the issue is in the build configuration. 
By default the compilation is in 32 bit (86x), this need to be changed to "Any CPU".
That's all. After this changes my code is working fine.


Tuesday, February 22, 2011

Fixing issue "Cannot connect to the configuration database"

One day on my Dev machine I received the following error while browsing my site. 


The I searched on the Google and find out this Microsoft KB article and followed each steps. Though it was written for WSS 2.0 but it's working fine for WSS 3.0 and MOSS. But following those steps does not helped much. 
While working on SQL Database, I found a wired thing that is SharePoint_Config Db is marked as 'Suspect'.
  


After further research, I found this blog to use the DB script to restore it back to the original condition.


EXEC sp_resetstatus 'DBname'
ALTER DATABASE DBname SET EMERGENCY
DBCC checkdb('DBname')
ALTER DATABASE DBname SET SINGLE_USER WITH ROLLBACK IMMEDIATE
DBCC CheckDB ('DBname', REPAIR_ALLOW_DATA_LOSS)
ALTER DATABASE DBname SET MULTI_USER



Final Issue: At the time of using this script I am getting sql error. Incorrect syntax near '-'. This error causing by the illegal characters in the DB name. As SharePoint adds GUID in the title of SharePoint config database.


We need to use a [bracket] for handling such issues. This Microsoft site got details about it.
use [SharePoint_AdminContent_d610f804-ebc5-4480-91a8-1f0a53de078a]



Tuesday, February 8, 2011

Common scenarios in Web Services of SharePoint 2007

Following are the common Web Services available in a SharePoint 2007. The physical location of these files is "C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI"



Administration Service             http:///_vti_adm/admin.asmx 
Alerts Service                          http:///_vti_bin/alerts.asmx 
Document Workspace Service   http:///_vti_bin/dws.asmx 
Forms Service                          http:///_vti_bin/forms.asmx 
Imaging Service                        http:///_vti_bin/imaging.asmx 
List Data Retrieval Service        http:///_vti_bin/dspsts.asmx 
Lists Service                            http:///_vti_bin/lists.asmx 
Meetings Service                      http:///_vti_bin/meetings.asmx 
Permissions Service                  http:///_vti_bin/permissions.asmx 
Site Data Service                      http:///_vti_bin/sitedata.asmx 
Site Service                             http:///_vti_bin/sites.asmx 
Users and Groups Service           http:///_vti_bin/usergroup.asmx 
Versions Service                       http:///_vti_bin/versions.asmx 
Views Service                          http:///_vti_bin/views.asmx 
Web Part Pages Service            http:///_vti_bin/webpartpages.asmx 
Webs Service                           http:///_vti_bin/webs.asmx 





Setting the web service user credentials
The SharePoint web services only accept calls from existing SharePoint users and do also enforce access security. Import the System.Net namespace into your project and then use the NetworkCredential class to set the user credential to use for the web service call. Here is a code snippet:


public static XmlNode VersionsGetVersions(string SharePointHost, string UserName,
                              string Password, string Domain, string FileName)
{
// proxy object to call the Versions web service
Versions.Versions VersionsService = new Versions.Versions();


// the user credentials to use
VersionsService.Credentials = new NetworkCredential(UserName, Password,
Domain);
VersionsService.Url = SharePointHost + “_vti_bin/Versions.asmx”;


// gets the file versions
XmlNode Result = VersionsService.GetVersions(FileName);


// dispose the web service object
VersionsService.Dispose();
return Result;
}


Following are some real life example of using SharePoint Web Services:



Example 1:  Get the collection of SharePoint lists, fields and views 
In the first example we want to get the collection of SharePoint lists. For each list we want to get all the defined list fields (fields you can use to store information) and finally all views associated with the list. Here are the web methods to call:


1) On the Lists web service call the GetListCollection() web method to get the collection of all SharePoint lists. This returns an XML document with all SharePoint lists. 
2) Next you run the “//sp:List” XPath query to get all matching List nodes. The Title attribute of each matching node contains the name of the SharePoint list. 
3) For each SharePoint list we call the GetList() web method on the Lists web service, passing along the list name. This returns a XML document with detailed information about the list including the list of fields. 
4) Next you run the “//sp:Field” XPath query to get all the matching Field nodes. The Name attribute contains the field name. 
5) For each SharePoint list we call the GetViewCollction() web method on the Views web service, passing along again the list name. This returns a XML document listing all views for the list. 
6) Finally you run the “//sp:View” XPath query to get all the matching View nodes. The Name attribute contains the name of the view. 


Example 2:  Get the list of users and site-groups
In this example we want to get the list of site users and to which site group each user belongs. We also want to get the list of site groups and which users belong to each site group.


1) On the Users-and-Groups web service we call the GetUserCollectionFromWeb() web method. This returns an XML document with all the site users. 
2) Next you run the “//d:User” XPath query to get all the matching User nodes. The Name attribute contains the user name and the LoginName attribute the user's login name. 
3) For each user we call the GetRoleCollectionFromUser() web method on the Users-and-Groups web service passing along the user's login name. This returns a XML document with all the site groups the user belongs to. 
4) Next you run the “//d:Role” XPath query to get all the matching Role nodes. The Name attribute contains the site group name. 
5) To get the list of site groups call the GetRoleCollectionFromWeb() web method on the Users-and-Groups web service. This returns an XML document with all site groups. 
6) Next you run again the “//d:Role” XPath query to get all the matching Role nodes. The Name attribute contains the site group name. 
7) Finally call for each site group the GetUserCollectionFromRole() web method on the Users-and-Groups web service passing along the site group name. This returns an XML document with all the users belonging to this site group. 
8) Next you run again the “//d:User” XPath query to get all the matching User nodes. The Name attribute contains the user name and the LoginName attribute the user's login name. 


Example 3:  Get the list of sites, site-templates and list-templates
With the last example we want to get a list of all sites in the site collection. We want to get for the site collection the list of site templates. Additionally we want for each site the list of list templates.


1) First we call the GetAllSubWebCollection() web method on the Webs web service. This returns an XML document with all sites in the site collection. 
2) Next run the “//sp:Web” XPath query to return all matching Web nodes. The Url attribute contains the absolute URL for the site. 
3) Then we call the GetSiteTemplates() web method on the Sites web service. This returns an array of available site templates in the site collection, which is an array of the type Sites.Templates. The attached sample application converts all structures to an XML document using reflection, so you can run XPath queries against it (see the method SharePoint.SitesGetSiteTemplates()). 
4) Next run the “//SharePointServices.Sites.Templates” XPath query which returns all matching template nodes. The Title attribute contains the template title and the Name attribute the SharePoint template name. 
5) For each site we call the GetListTemplates() web
method on the Webs web service. Before calling the web service object you need to set the URL to the site URL (returned by GetAllSubWebCollection()). This way we make sure that the call is to the site itself and returns the list templates of that site. This returns an XML document with all list templates. 
6) To finish run the “//sp:SiteTemplate” XPath query to return all matching SiteTemplate nodes. The DisplayName attribute contains the name of the list template.





Thank you,
Soumyendra