Monday, November 8, 2010

SPQuery on SPView gives an exception "Value does not fall within the expected range"

Inorder to get the results from the specific view of a list where the view is created with some filter conditions SPQuery should be run on spcific view (SPView) of a list instead default list.
SPList spList = spWeb.Lists["List Name"];
SPView view = spList.Views["View Name"];
SPQuery query = new SPQuery(view);
SPListItemCollection collection = spList.GetItems(query);
The statement executes and gets the result collection, but the result item(listitem) of collection doesn't have the properties or columns loaded by default.

So collection[0]["Title"] gives "value does not fall within the range" exception.

To avoid the exception, all the fields which are going to be read in the further statements should be specified for the view as viewfields.
query.ViewFields = "
Similarly 'n' number of fields which are going to be used in further can be specified as view fields.

Friday, July 16, 2010

SharePoint overriding the Core.css styling for specific site

The standard default core.css file is located in …\12\TEMPLATE\LAYOUTS\1033\STYLES. This STYLES folder is a virtually mapped via the /_layouts virtual directory that is mapped to each and every site in SharePoint.  So, the core.css file is accessible to every site by simply using the URL of “/_layouts/1033/styles/core.css”.  For every page that SharePoint renders, it renders a link to the core.css using this path. 

However, for any site that has been configured to use a custom core.css style sheet via the CustomizeCss method on the SPWeb class, the URL rendered in each page references a core.css file located in a different location.  This is accomplished in C# by the following code where web is an instance of the SPWeb class for the web site of your choice.


The CustomizeCss(“core.css”) statement tells SharePoint to use a custom copy of the SharePoint core.css file.  Once this CustomizeCss method is executed, a copy of the core.css file found in the …\12\TEMPLATE\LAYOUTS\1033\STYLES folder is copied to the folder (access this by Sharepoint Designer) named "_styles" on the applied site.  

Modifying this core.css file will affect only the current site. 

Reverting this site back to using the original global (un-customized) copy of the core.css file can be accomplished just a easy using the following code.


Executing the RevertCss method does not remove the custom core.css file on the web site’s “/_styles” folder.  It simply tells the site to no longer use it and to use the original core.css found in the C:\…\12\TEMPLATE\LAYOUTS\1033\STYLES folder; which is the same folder as the as the “/_layouts/1033/styles” folder.

So we now know how to use the CustomizeCss and the RevertCss methods on the SPWeb class to provide an easy way to point the site to a new custom style sheet file.

SharePoint libraries Types and Values

Following is a description for the available types (available in the WSS SDK)

Value Description
100 Generic list
101 Document library
102 Survey
103 Links list
104 Announcements list
105 Contacts list
106 Events list
107 Tasks list
108 Discussion board
109 Picture library
110 Data sources
111 Site template gallery
113 Web Part gallery
114 List template gallery
115 XML Form library
120 Custom grid for a list
200 Meeting Series list
201 Meeting Agenda list
202 Meeting Attendees list
204 Meeting Decisions list
207 Meeting Objectives list
210 Meeting text box
211 Meeting Things To Bring list
212 Meeting Workspace Pages list
300 Portal Sites list.
1100 Issue tracking
2002 Personal document library
2003 Private document library


0 — Custom List
1 — Document Library
2 — Not used
3 — Discussion Forum
4 — Surveys
5 — Issues List

so, if you are developing custom picture library set Type="109" and BaseType="1" (because picture library mainly based on document library)

Wednesday, May 5, 2010

SharePoint Document Library Open Excel File in Browser

The excel file uploaded to the document library (Excel) by default will be opened in the Microsoft Office Excel Application. To open any newly added excel file in browser, following configuration is required.

Open Library --> Settings --> Document Library Settings --> Advanced Settings (under general settings) --> Browser-Enabled Documents section --> Display as web page --> hit OK button

Then the excel file which needs to be open in browser must be published to the library rather than uploading directly. To publish the excel file follow the below steps.

Open Excel File --> Office Button --> Publish --> Excel Services
Then a dialog box opens and asks for the file name to save at specific location shown in address bar of that dialog box.
To publish the file in document library type in the URL of the document library URL (http://site/library name) in the address bar and hit ENTER. Now the document should be saved in the library.

Note: Publishing via Excel services unlocks the possibility of making available named cells as parameters to the excel sheet on the web. Named cells can be exposed as parameters by clicking on the "Excel Services Options" box in Microsoft Office Excel 2007, during the publish process. Cells can be named using the DefineName thing on the Formula ribbon/bar. Also, if you wish to limit the publishing to specific sheets, or part of a sheet - you need to use a version of Office that has the ability to publish to Excel services.

After the above settings if the published excel file is tried to open in a web page it gives the following error.
Error : You do not have permissions to open this file on Excel Services.
Make sure that the file in an Excel Services trusted location and that you have access to the file.

To resolve the error/issue few more settings need to be changed/configured in Central Administration. To do so follow the below steps.
1. In SharePoint Central Administration, go to 'Shared Services Administration'
2. Select your shared service. (Default is SharedServices1)
3. In 'Excel Services Settings' section select 'Edit Excel Services settings'.
4. Ensure that File Access method is Process Account. Click OK.
5. Back in 'Excel Services Settings' section and select 'Trusted File Locations'
6. Add a new Trusted file Location:
    URL: Specigy the specific library or the site URL in which the library exists
    Location Type: Windows SharePoint Services
    Trust Children: Children Trusted checkbox should be ON/YES
7. Reset IIS

All the documents published to the library with above settings will be opened in browser when the users clicks on the link of the Excel file in library.

Infopath "Request for the permission of type 'Object' failed" exception in VSTA

If you are creating a InfoPath form with managed code(c# or VB), the form should have full trust (security permission).
If the trust level is not full it gives an exception "Request for permission of type 'object' failed" for the object.
Below code in VSTA with infopath form having default settings gives an exception "Request for the permission of type 'System.Net.Mail.SmtpPermission' failed".
MailMessage MailMsg = new MailMessage();
MailAddress fromAddress = new MailAddress("", "FromName");
MailMsg.From = fromAddress;
MailAddress toAddress = new MailAddress(toEmail, toName);
MailMsg.IsBodyHtml = true;
MailMsg.Subject = subject;
MailMsg.Body = body;
SmtpClient smtpClient = new SmtpClient("");

What it really needs to overcome the above issue is to give full trust level (security permission).
The full trust level can be given for infopath form changing the form settings as follows.
1) Open infopath form  2)Go to Tools menu  3)Open Forms Options  4)Go to Security and Trust tab 5)Deselect automatically determine security level check box  6) Select Full Trust radio button  7)Hit OK button

The setting itself solves the problem.

SharePoint Making the Title as required field in Document/Picture library

Document Library has Name as the required field, whereas Title is not. Now making the Title field as required in a Document/Picture Library.

Navigate to the Document/Picture Library Settings and enable it by managing content type.

1. Go to Document/Picture Library Settings
2. Click on Advanced Settings
3. Select 'Yes' for the option 'Do you want to manage the content types?' and Save
4. Click on 'Document' link under Content Types section
5. You will see all the columns listed
6. Click on 'Title' and now you can make it required field.
7. Save the changes
By default, the title field doesn’t contain any validation in Document/Picture Library. If it needs to be made as required one, schema.xml of “Document/Picture Library” needs to be customized, which can be found at the following location.
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\Document or Picture Library\DocLib or PicLib
1. Open the schema.xml from the above location
2. Locate the “Title” field and add the attribute - Required="TRUE" to the tag.

Required="TRUE" SourceID="" StaticName="Title">
3. Install and activate the feature. Now title field asks for value as it is required.

SharePoint Delete the Title field from Document or Picture Library

“Title” column of a Document/Picture library through object model can’t be deleted and gives an error. This is because the “CanBeDeleted” property of that field has the value “false”. The column will not be deleted even if “AllowDelete” property is set to “true”, after this it gives another error saying that “sealed property can’t be deleted”. To delete the column, schema.xml of “Document/Picture Library” needs to be customized, which can be found at the following location.
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\Document or Picture Library\DocLib or PicLib
1. Open the schema.xml from the above location
2. Locate the “Title” field and change the sealed attribute value as Sealed="TRUE" to the tag.
Sealed="TRUE" SourceID="" StaticName="Title" ColName="nvarchar7"/>

Code for deleting the Title Field:-
using(SPSite siteCollection = new SPSite ("Web Application"))
   using(SPWeb parentWeb = siteCollection.OpenWeb())
      SPList oList = parentWeb.Lists["Document/Picture Library Name"];
      SPField oField = oList.Fields["Title"]; //Column name
      oField.AllowDeletion = true;

SharePoint Activate/Enable site's mobile view

Scinario: If the mobile view (default) for each site/sub site is not activated, it gives 404 error when the site is accessed in mobile view (http://Site URL/m).

Solution: Activate MobilityRedirect feature (it is not activated by default) through the command line as it is hidden.

stsadm -o activatefeature -name MobilityRedirect -url http://Site URL/

To test if it is activated browse to http://Site URL/m/
If it is working it will redirect you to a page of mobile view, otherwise it will gives a 404 error.

SharePoint Mobile view of the site/sub site

The mobile view of the SharePoint site/sub site can be accessed by adding 'm' after the site/sub site in the url.
1. The mobile home page has a short URL with an "m" folder appended to the end of the regular URL (for example, http://Site URL/m/) that redirects the request to the mobile view's default.aspx page
2. The default.aspx page then redirects the user to the actual home page (http://site or sub site/_layouts/mobile), according to the current site definition type
3. To access this, 'mobile view' needs to be activated for site/sub site. For activation details follow the link.

SharePoint List’s Person or Group field value as c# SPUser objet

Casting the list's Person or Group field value into SPUser object.
using(SPSite spSite = new SPSite("site URL"))
   using(SPWeb spWeb = spSite.OpenWeb())
      SPList spList = spWeb.Lists["List Name"];
      SPListItem spListItem = spList.GetItemByID(itemID);
      string currentValue = spListItem["Person or Group field name"].ToString();
     SPFieldUser userField = (SPFieldUser)spWeb.Lists["List Name"].Fields.GetField("Person or Group field name");
     SPFieldUserValue fieldValue = (SPFieldUserValue)userField.GetFieldValue(currentValue);
     SPUser user = fieldValue.User;


     SPFieldUserValue spFieldUserValue = new SPFieldUserValue(web ,  Convert.ToString(projectItem["Person or Group field name"]));
     SPUser user =  spFieldUserValue.User;

SharePoint Change the search button image

To change the search button static(default)  image, attributes
GoImageUrl="image relative path" GoImageUrlRTL="image relative path" needs to be changed.

To change the button hover image, attributes
GoImageActiveUrl="image relative path" GoImageActiveUrlRTL="image relative path" needs to be changed.

*image relative path - /images/search.gif

Tuesday, May 4, 2010

STSADM commands for Backup and Restore Site Collection/Site/Sub Site

stsadm commands backup and restore are to backup/restore the site collection only and not for site/sub site.
stsadm -o backup -url  -filename 
stsadm -o restore -url  -filename  -overwrite
Inorder to to do the backup and restore operations on sites or sub sites stsadm commands import and export are used respectively.

Operation name: Export
Exports site and sub site data from your Microsoft Office SharePoint Server 2007 installation.

stsadm -o export -url  -filename 
[-versions] <1-4>
Operation Name: Import
Imports site and sub site data from your Microsoft Office SharePoint Server 2007 installation.

stsadm -o import -url  -filename 
[-updateversions] <1-3>