Showing posts with label SP 2010. Show all posts
Showing posts with label SP 2010. Show all posts

Monday, April 16, 2012

How to refer custom.css into SharePoint 2010 sites

This time I got to create my custom css for my SharePoint 2010 custom master page. Now, how to add/refer my custom css?

The Solution is simple, refer in the following way:

<SharePoint:CssRegistration ID="CssRegistration1" Name="/Style Library/Home/CSS/mycustom.css" runat="server" After="corev4.css" /> 

SharePoint 2010 uses corev4.css, located at /layouts/1033/styles/Themable/corev4.css on the server.

We can customize CSS just as we could on MOSS 2007, but in SharePoint 2010 we will customize it in the core4.css file. Unlike MOSS, when we customize CSS for a SharePoint 2010 site, the _Styles folder is not automatically created on the current site. When customizing CSS in SharePoint 2010, the _Styles folder is gotten from folder 14 on the server and runs from there.

Here are some of the most common ways to reference custom CSS for SharePoint 2010. One key change over 2007 is the ability to specify After=”corev4.css” in the CssRegistration to make sure your custom CSS is referenced after the OOTB corev4.css file.

<SharePoint:CssLink runat="server" Version="4"/>
Note: Default OOTB
Output Order:

  1. <link rel="stylesheet" type="text/css" href="/_layouts/1033/styles/Themable/corev4.css"/>

<SharePoint:Theme runat="server"/>
Note: If a theme is enabled
Output Order:

  1. <link rel=”stylesheet” type=”text/css” href=”/_themes/[UniqueCounter#]/corev4-[u=UniqueHex#].css?ctag=[UniqueCounter#]”/>

<SharePoint:CssRegistration name=”customfolder/samplecustom.css” runat=”server”/>
Note: Alternate CSS on Server but comes before corev4.css
Output Order:

  1. <link rel="stylesheet" type="text/css" href="/_layouts/1033/styles/customfolder/samplecustom.css"/>
  2. <link rel="stylesheet" type="text/css" href="/_layouts/1033/styles/Themable/corev4.css"/>

<SharePoint:CssRegistration name=”customfolder/samplecustom.css” After=”corev4.css” runat=”server”/>
Note: Alternate CSS on Server and comes after corev4.css.
Output Order:

  1. <link rel="stylesheet" type="text/css" href="/_layouts/1033/styles/Themable/corev4.css"/>
  2. <link rel="stylesheet" type="text/css" href="/_layouts/1033/styles/customfolder/samplecustom.css"/>

<SharePoint:CssRegistration name="<% $SPUrl:~sitecollection/Style Library/samplecustom.css %>" After="corev4.css" runat="server"/>
Note: Alternate CSS In SharePoint Database for sites without publishing enabled
Output Order:

  1. <link rel="stylesheet" type="text/css" href="/_layouts/1033/styles/Themable/corev4.css"/>
  2. <link rel="stylesheet" type="text/css" href="/Style%20Library/emc_2010_custom.css"/>

<SharePoint:CssRegistration name="<% $SPUrl:~sitecollection/Style Library/~language/Themable/Core Styles/samplecustom.css %>" After="corev4.css" runat="server"/>
Note: Alternate CSS In SharePoint Database for sites with publishing enabled
Output Order:

  1. <link rel="stylesheet" type="text/css" href="/_layouts/1033/styles/Themable/corev4.css"/>
  2. <link rel="stylesheet" type="text/css" href="/[sitename]/Style%20Library/en-US/Themable/Core%20Styles/emc_2010_custom.css"/>

<link rel="stylesheet" type="text/css" href="/_layouts/1033/styles/customfolder/samplecustom.css"/>
Note: Alternate CSS on Server by using the standard link rel. Does not need the After attribute…
Output Order:

  1. <link rel="stylesheet" type="text/css" href="/_layouts/1033/styles/Themable/corev4.css"/>
  2. <link rel="stylesheet" type="text/css" href="/_layouts/1033/styles/customfolder/samplecustom.css" />

 

If you have a more elegant solution – please post a comment… I’ll be happy to hear.
...HaPpY CoDiNg

Partha (Aurum)

Ref:

http://erikswenson.blogspot.com/2010/01/sharepoint-2010-base-css-classes.html

http://community.bamboosolutions.com/blogs/sharepoint-2010/archive/2010/04/02/branding-in-sharepoint-2010.aspx

http://msdn.microsoft.com/en-us/library/gg430141.aspx

Friday, April 13, 2012

SharePoint Error and Exception handling

In this blog I will provide some basics, how we can make our SharePoint code more robust and proofing against any error or exception.

The Basics

1.     Always keep null checking

2.     Always log an error when it occurs and write your log message in detail around the specific error.

3.     Always catch the specific exception.  Avoid catch the generic exception (i.e. System.Exception Object, I know we developer do this very often). The issue here is that although you have caught the exception it still becomes very difficult to determine what really cause the problem because the error message will be generic.

4.     Never throw an exception inside a constructor as it can be very difficult to find where the exception is being thrown and always catch all exceptions in a Dispose method if you are defining your own.

5.     Never use try catch blocks to control the logic of code, it is more efficient to use if/else statements and only throw exceptions when nothing else can be done.

6.     Try/Catch blocks should never contain empty catch handlers.

Logging

An important part of exception handling is providing detailed information around the error in order to trouble shooting errors that may arise during development, UAT and/or Production. Generally, when developing a SharePoint (MOSS and SP2010) solution I like to use the SharePoint logger (i.e. PortalLog) so as to keep all diagnostic and error information in one log file (i.e. in the 12/14 hive under the LOGS directory) and no extra dll’s are needed to be installed. The only limitation to using the MOSS logger is that we cannot set the log event level e.g. critical, high, medium, low it will always display the error level as high. The MOSS logger uses the PortalLog class and logs a message by calling the LogString method as shown below:

catch (Exception ex)

            {

                Microsoft.Office.Server.Diagnostics.PortalLog.LogString("Exception Occured in Left Menu - Control :" + ex.Message);

            }

 

The MOSS logger is located in the Microsoft.Office.Server.dll. If you want more control and flex ability use the Enterprise library logger.

 

SharePoint Exception handling

1.      In SharePoint always throw SPException Objects to the top of the UI call stack and pass user friendly messages to the SPException object as this will be the message displayed to the user in the browser. Do not pass the Message property of the specific exception into the SPExcetption object as the user may not understand such a technically messages. Below is an example within a receiver, catch the specific exception, log the error and throw a SPException object to the top of the UI stack as shown below

public override void FeatureActivated(SPFeatureReceiverProperties properties)

        {

            try

            {

                // Do Something

            }

            catch (ArgumentException ex)

            {

 Microsoft.Office.Server.Diagnostics.PortalLog.LogString("Exception Occured in Left Menu - Control :" + ex.Message);

               throw new SPException("Check Left Menu or Left Menu String");

            }

        }

 

If you only catch and log the exception without throwing the SPException  then the  feature will complete the requested feature  activation or de-activation (which ever the user is currently requesting) instead of aborting the feature activation or de-activation.

2.     When developing a custom feature receiver in which you are obtaining an SPSite or SPWeb object via the Parent object make sure you check if the parent object can be casted to a SPSite or SPWeb object (depending on the scope of your feature) before casting it. An example is provided below.

public override void FeatureActivated(SPFeatureReceiverProperties properties)

        {

            try

            {

                Object parent = properties.Feature.Parent;

                if (parent is SPWeb)

                {

                    SPWeb spWeb = (SPWeb)parent;

                }

                else

                {

                  PortalLog.LogString("Log exception");

                  throw new SPException("Enter user friendly message");

                }

            }

            catch (ArgumentException ex)

            {

               Microsoft.Office.Server.Diagnostics.PortalLog.LogString("Exception Occured in Left Menu - Control :" + ex.Message);

             throw new SPException("Check Left Menu or Left Menu String");

            }

        }

3.     When developing webparts which have custom user defined properties available in the web part editing pane, the webpart should always check the value and if it is not valid display an error message in the web part. This applies to the situation when a user changes the value in the web part editing pane. If the value is invalid the user should be notified after they click apply, so they can immediately change the value to a correct value.

4.     When developing web parts that depend on data from a SPLists you should always catch the UnauthorizedAccessException object that will be thrown if the user does not have permissions. Web parts will be accessed by many different people with different permissions and in some situations users may not have the required permissions for particular lists. The developer may want to catch the exception and display a message in the web part or implement some type of action i.e. display a request for access control. In addition, you will also have to set SPSecurity.CatchAccessDeniedException = false so that the platform does not catch the access denied exception.

 

5.     When accessing lists (SPList) or list items (SPListItem) always catch the appropriate exception which will be thrown if the list or list item does not exist. If you access a list a list or list item via the collection key and the list or list item does not exist an ArgumentExecption will be thrown. This is illustrated below.

try

{

   spList = spWeb.List[“My WebSite”];

   // or

   spListItem = spList.Items[“My Item”];

}

catch(ArgumentException)

{

   // Log and take appropriate action for the exception

}

 

If you access a list or list item via the relative server URL and the list or list item does not exist a FileNotFoundExecption object will be thrown . This is illustrated below.

try

{

   spList = spWeb.GetList(“/Lists/MyList”);

   // or

   spListItem = spWeb.GetListItem(“/Lists/MyList/MyItem”);

}

catch(FileNotFoundException)

{

   // Log and take appropriate action for the exception

}

 

Otherwise, in some situations you may want to check if the list item exists before accessing it, an example is shown below.

 

if(spList.Fields.ContainsField(“MyItem”);

{

   spListItem = spList.Items[“My Item”];

}

else

{

   // Take appropriate action

}

 

However if you want to check if a list exists you will have to enumerate through the  web site’s Lists collection and check if it exits, there is no method in the Object model that does this so you may want to extend the SPList class to accommodate this.

 

6.     Never dispose of SPSite or SPWeb object inside of a try block, if an exception gets thrown before the Dispose method it will not dispose of the object appropriately, leading to memory leaks,. Best practice is to wrap your code in a using block when creating these objects, for example

Using (SPSite site collection = new SPSite(http://localhost)

{

   // Do something

}

7.     When checking out items make sure that your code can roll back changes or check the file back in when an exception occurs. Otherwise the item will remain in a checked out state and users will not be able to use that particular file. For example

try

{

 file.CheckOut();

 // Do something to the file

 File.CheckIn("comment");

}

finally

 {

  if (file.CheckOutStatus != SPCeckOutStatus.None)

  file.UndoCheckOut();

 }

If you have a more elegant solution – please post a comment… I’ll be happy to hear.
...HaPpY CoDiNg

Partha (Aurum)

Thursday, March 29, 2012

Error occurred in deployment step ‘Activate Features’: Cannot start service SPUserCodeV4 on computer

If you ever try to deploy a sandboxed solution and run into Error occurred in deployment step ‘Activate Features’: Cannot start service SPUserCodeV4 on computer, this only means that the Windows service running sandboxed solutions is not running on the server.

To start the service go to SharePoint Central Administration, go to System Settings tab and click on Manage services on server:
clip_image001

In the page that opens start Microsoft SharePoint Foundation Sandboxed Code Service:
clip_image002

That’s all! Your solution should deploy just fine now.

If you have a more elegant solution – please post a comment… I’ll be happy to hear.

...HaPpY CoDiNg

Partha (Aurum)

Lock or unlock SharePoint 2010 site collections

I faced this issue today. After a backup restore, my site collection started behaving differently. It was a team site with publishing features turned on.

After the restore, the additional menus, disappeared, faced access denied etc. Finally found out that the Site Collection has got locked after restore operation.

 

In this blog I will describe how to lock and unlock Site Collection. You can apply locks to a site collection to prevent users from updating content or you can temporarily prevent users from accessing the site collection.

 

Option

Description

Not locked

Unlocks the site collection and makes it available to users.

Adding content prevented

Prevents users from adding new content to the site collection. Updates and deletions are still allowed.

Read-only (prevents additions, updates, and deletions)

Prevents users from adding, updating, or deleting content.

No access

Prevents users from accessing the site collection and its content. Users who attempt to access the site receive an error.

1.     In Central Administration, click Application Management.

2.     On the Application Management page, in the Site Collections section, click Configure quotas and locks.

clip_image002

3.     If the site collection you want isn't already selected, in the Site Collection section, on the Site Collection menu, click Change Site Collection. Use the Select Site Collection page to select a site collection.

clip_image004

If you have a more elegant solution – please post a comment… I’ll be happy to hear.

...HaPpY CoNfIgUrInG

Partha (Aurum)

Reference:

http://technet.microsoft.com/en-us/library/cc263238.aspx

Wednesday, March 14, 2012

Step-by-Step: Create the Search Service Application in SharePoint 2010

 

In my current project, I got the opportunity to set up the SharePoint 2010 farm creation. In this blog I will only talk about the way I configured the Search Service application. There are numerous links/contents available, this piece only talks about what I did. If I have missed any points please put your comments. Hope to talk about more configurations in coming blogs….

When we deploy and configure a Search service application, we perform the following basic steps:

 

  1. Create accounts – Domain user accounts are required to run the various search services and components.
  2. Create a Search service application – A Search service application provides enterprise search features and functionality.
  3. Configure the Search service application – Basic configuration of a Search service application includes configuring a default content access account, an e-mail contact, and content sources.
  4. Configure the Search topology – You can deploy crawl components, query components, and the administration component on different servers in the farm. You can also specify which instance of SQL Server is used to host the administration, crawl, and property databases.

 

1.1    Create Accounts

 

The below Accounts are required to configure search service application

#

Account

Use

1

Search service application

SharePoint Foundation Search Service Account: “spSearch”

The Windows user account that is used for the SharePoint Server Search service, the Search Admin Web Service application pool, and Search Query and Site Settings Web Service application pool.

2

Default content access

SharePoint Foundation Search Content Access Account:

“spSearchCA”

The identity that is used by the Search service application to access content when crawling.

 

1.1.1     Register Managed Accounts

The service account for search (the account used for the Search service account, the Search Admin Web Service, and the Search Query and Site Settings Web Service) must be registered as a managed account in SharePoint Server 2010 to make it available when you create the Search service application. Use the following procedure to register this account as a managed account.

#

Description

1

On the Central Administration home page, in the Quick Launch, click Security

2

On the Security page, in the General Security section, click Configure managed accounts

3

On the Managed Accounts page, click Register Managed Account (the AD users should have “Read” access permission defined in AD)

 

4

On the Register Managed Account page, in the Account Registration section, type the user name and password that you want to use as credentials for the service account

clip_image002

5

If you want SharePoint Server 2010 to manage password changes for this account, select the Enable automatic password change check box and configure the parameters for automatic password change

 

1.1.2     Search Database Architecture

#

Description

1

Architecture (Databases)

When you create a Search service application, SharePoint Server creates three databases for storing search information and associated data:

·         Administration Database – used to store search admin information

·         Crawl database – used to store crawl information

·         Property Database – used to store index information

 

 

1.2    Create a Search service application

 

You can create multiple Search service applications if you have to have multiple search indexes.

 

#

Description

1

Verify that the user account that is performing this procedure is a member of the Farm Administrators group for the farm for which you want to create the service application

2

On the Central Administration Home page, in the Application Management section, click Manage service applications

3

On the Manage Service Applications page, on the ribbon, click New, and then click Search Service Application

clip_image004

4

On the Create New Search Service Application page, accept the default value for Name, or type in a new name for the Search service application.

5

In the Search Service Account list, click the account that you created in the previous procedure to run the Search service

6

In the Application Pool for Search Admin Web Service section, do the following:

1.     Select the Create new application pool option, and then type a name for the application pool in the Application pool name text box.

2.     In the Select a security account for this application pool section, select the Configurable option, and then select the account that you created to run the application pool for the Search Admin Web Service from the list.

7

In the Application Pool for Search Query and Site Settings Web Service section, do the following:

1.     Choose the Create new application pool option, and then type a name for the application pool in the Application pool name text box.

2.     In the Select a security account for this application pool section, select the Configurable option, and then select the account that you created to run the application pool for the Search Query and Site Settings Web Service from the list.

 

8

Click OK on the new Create New Search Service Application dialog to provision the new service application.

9

Once the Search Service Application has been successfully provisioned on the server farm you will have a topology with 1 Search Administration, 1 Crawl, and 1 Query component on the machine hosting SharePoint 2010 Central Administration and all associated databases on the default database server.

 

 

1.3    Configure the Search service application

 

You configure a Search service application on the Search Administration page for that service application.

 

Use the following procedure to navigate to the Search Administration page for a particular Search service application.

 

#

Description

1

Verify that the user account that is performing this procedure is an administrator for the Search service application that you want to configure.

2

On the Home page of the Central Administration Web site, in the Application Management section, click Manage service applications.

On the Manage Service Applications page, click the Search service application that you want to configure. Go to the Search Administration page

clip_image006

4

On the Search Administration page, configure the following settings as described in the following sections:

  • Default content access account
  • Contact email address
  • Content sources

 

5

Specify the default content access account

When you create a Search service application, the account that you specify for the Search Service Account is automatically configured as the default content access account. The crawler uses this account to crawl content that does not have an associated crawl rule that specifies a different account. For the default content access account, we recommend that you specify a domain user account that has read access to the content that you want to crawl. You can change the default content access account at any time.

 

If you need to crawl certain content using a different account, you can create a crawl rule and specify a different account for crawling. For information about how to create a crawl rule, see Manage crawl rules (SharePoint Server 2010).Use the following procedure to specify the default content access account.

To specify the default content access account

1.     On the Search Administration page, in the System Status section, click the link in the Default content access account row.

2.     In the Default Content Access Account dialog box, in the Account box, type the account that you created for content access in the form domain\user name.

3.     Type the password for this account in the Password and Confirm Password boxes.

4.     Click OK.

6

Specify the contact e-mail address

The contact e-mail address is written to the logs of crawled servers. The default contact e-mail address, someone@example.com, is a placeholder. We recommend that you change this to an account that an external administrator can contact when a crawl might be contributing to a problem such as a decrease in performance on a server that the search system is crawling.

Use the following procedure to specify the contact email address.

To specify the contact e-mail address

1.     On the Search Administration page, in the System Status section, click the link for the Contact e-mail address.

2.     In the Search Email Setting dialog box, in the E-mail Address box, type the e-mail address that you want to appear in the logs of servers that are crawled by the search system.

3.    Click OK.

7

Create content sources

Crawling requires at least one content source. A content source called Local SharePoint sites is created automatically during installation and is automatically configured for crawling all of the SharePoint sites in the server farm. If you perform a Server Farm installation of SharePoint Server 2010, no crawls are automatically scheduled or performed.

1.     Click the Content Sources from quick launch menu

2.     Click Local SharePoint Sites (otherwise you can create New Content Source)

3.     Add the url in “Start Address”

4.     Click “Start all crawls” from the Content Sources page.

 

 

1.4    Configure the Search topology

 

The search topology covers the below:

 

The Search administration (Admin) topology does not scale out - there can be on one (1) search administration component and one (1) search administration database per Search Service Application.

 

The Crawl topology can be scaled out by adding Crawl Components or Crawl Databases.  Crawl Components can have a many-to-one relationships with Crawl Databases.

 

The Query topology can be scaled out by adding Property Databases or by adding Query Components.  Index Partitions subdivide the full-text index.   A new Query Component can either be the first component in a new partition or an additional component in an existing partition.

 

The next step is to move the query components to the servers on the Web tier, the crawl and property store (query) databases are provisioned on the SQL server instance that was specified for the SharePoint Server farm. If you have more than one application server in your farm, you can deploy these components on other application servers, depending on your requirements. You can deploy multiple instances of each crawl component and each query component.

 

#

Description

1

To move the query component to a Web server (with query component in 2 WFEs)

1.     On the Central Administration Home page, click Manage service applications.

2.     On the Services Applications page, click Search Service Application.

3.     On the Search Administration page, in the Search Application Topology section, click Modify.

4.     On the Topology for Search Service Application: Search Service Application page, in the Index Partition section, click Query component 0, and then click Edit Properties.

5.     On the Edit Query Component page, select one of the front-end Web servers from the Server drop-down list, and then click OK.

6.     On the Topology for Search Service Application: Search Service Application page, in the Index Partition section, click Query component 0, and then click Add mirror (for fail over).

7.     On the Add mirror query component page, in the Server drop-down list, select the front-end Web server that you identified on the Edit Query Component page, and then click OK.

2

To move the crawl component to a Web server

1.     On the Central Administration Home page, click Manage service applications.

2.     On the Services Applications page, click Search Service Application.

3.     On the Search Administration page, in the Search Application Topology section, click Modify.

4.     You can edit or create a new crawl component and select the App Server.

5.     Finally click Apply Topology Changes

3

Finally the Topology would look like below:

 

clip_image008

 

Lot more links are available in TechNet for further reading. My effort is just to bring in a minimums here.

Thanks for reading. If you have some other explanation – please post a comment… I’ll be happy to hear.

...HaPpY  AdMiNiStERiNg

Partha (Aurum)

Configuring SharePoint 2010 Search Service Application

Ref:

http://technet.microsoft.com/en-us/library/gg502597.aspx

http://technet.microsoft.com/en-us/library/ee805948.aspx

http://sharepointgeorge.com/2010/configuring-enterprise-search-sharepoint-2010/

http://blogs.technet.com/b/wbaer/archive/2009/11/23/step-by-step-provisioning-the-search-service-application.aspx