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

Sunday, January 30, 2011

Impersonation in Event Receiver

Recently faced an issue while working with Event Receivers. That is, current user need to impersonate with higher privilege account. Normally we do that with application pool account. 


Th problem here is, if you try to use event receiver's 'property' object to access the current web or the list item, the impersonation never works there. For example, following piece of code will use current user credentials, not the application pool account permission.


SPSecurity.RunWithElevatedPrivilege(delgate
{
properties.ListItem.Delete();
});


For using impersonation, you need to create a separate instances of SPWeb, then access your items.



SPSecurity.RunWithElevatedPrivilege(delgate
{
  using(SPSite site = new SPSite(properties.WebUrl))
  {
     using(SPWeb web = site.OpenWeb())
     {
         //access your items here.
     }
  }
});



Monday, December 13, 2010

Logging error message in SharePoint Programmatically

To log an error message from your custom code, first you need to take reference of 
"Microsoft.Office.Server.dll". From this dll we will use the diagnostics features.


Namespace:
Microsoft.Office.Server.Diagnostics


Next, following method need to be used for logging a message in SharePoint Log.



         private void LogULS()
        {
            PortalLog.LogString("A test Error Message.");
        }


The log can be access from 
"C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\LOGS" directory.



Friday, November 12, 2010

Creating a SandBox solution in SharePoint 2010

The Sandbox solution is newly added in SharePoint 2010, and it is very useful particularly for the Administrators to track each of the custom development object. 


Now, the administrator can monitor the performance of the custom application and that can be removed if more server resources are consumed. Another important thing is, the Sandbox solution run on a separate process thread SPUserCodes. This will stop taking SharePoint resources already using W3WP process.


For every site, there is a solution gallery, where already installed sandbox solutions are present.













Steps for creating the solution:


1) Create a blank solution in VS 2010, and specify the site URL and select "Sandbox solution"


2) Add a webpart, by click on project -> New Items
Please Note, the Visual Webpart is not supported in a Sandbox solution.


3) Write you custom codes for your webpart in the "CreateChildControl" method.


4) Build the solution and locate the WSP file in the Bin folder.


5) Now, open Power Shell and execute these commands for adding and installing the solution.


Add-SPUserSolution -LiteralPath "Physical path to WSP" -Site "Your site Url"


Install-SPUserSolution -Identity "WSP name" -Site "Your site url"
































Remember: Run the Power Shell as an administrator.

Friday, November 5, 2010

Static Navigation Menu on a Site Collection

Here, I am trying to change the built-in top navigation menu with a custom made menu. These menu items are static in nature and controlled from a XML file. 


These are the steps need to be followed:


1) Create a XML file and save the file as .sitemap


< ?xml version="1.0" encoding="utf-8" ? >
< siteMap >
  < siteMapNode title="Company" url="" >
    < siteMapNode title="Profile" >
      < siteMapNode title="About Us" url="/Default.aspx" >
        < siteMapNode title="History" url="/division1/division1a/Default.aspx" >
          < siteMapNode title="Management" url="/division1/division2a/Default.aspx"/ >
        < /siteMapNode >
        < siteMapNode title="News" url="/divi/division3a/Default.aspx" >
          < siteMapNode title="Media" url="/dion1/division3a/Default.aspx"/ >
        < /siteMapNode >
      < /siteMapNode >
    < /siteMapNode >
    < siteMapNode title="Interests" >
      < siteMapNode title="Careers" url="/division1/Home.aspx" >
        < siteMapNode title="Media" url="/divisi/division1a/Default.aspx"/ >
        < siteMapNode title="News" url="/divis/division2a/Default.aspx"/ >
      < /siteMapNode >
    < /siteMapNode >
  < /siteMapNode >
< /siteMap >



2) Place the file in _app_bin directory
(eg: C:\Inetpub\wwwroot\wss\VirtualDirectories\555\_app_bin\mainmenu.sitemap)


3) Open the web.config file of the corresponding site
(eg c:\Inetpub\wwwroot\wss\VirtualDirectories\555)


4)  Look for the "< siteMap" tag, and under "< providers >" add the following entry: and save the file.


< add name="CustomXmlContentMapProvider" siteMapFile="_app_bin/CustomSiteMap.sitemap" type="Microsoft.SharePoint.Navigation.SPXmlContentMapProvider, Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" / >


5) Open the site on SharePoint Designer, and add the following inside a SharePoint:DelegateControl



     < asp:SiteMapDataSource
ShowStartingNode="true"
SiteMapProvider="CustomXmlContentMapProvider"
id="xmlSiteMap"
runat="server"/ >




6) Then look for "< SharePoint:AspMenu ID='TopNavigationMenu'
and change these:


DataSourceID="xmlSiteMap"
MaximumDynamicDisplayLevels="4" 


The MaximumDynamicDisplayLevel indicates up to how many level the menu will expand.

7) The final output will be look like this: