Tuesday, February 22, 2011

Visual Studio Productivity Tool–Latest Update Released

Today Microsoft has released latest update to Visual Studio Productivity Tools. This update includes many improvements, if you like to know more about it, read the following blogs:

Enjoy it!!

Visual Studio is SIMPLY THE BEST!!!

Saturday, February 19, 2011

Supported Paths In-Place Upgrade

Simple rule! Take a look at this diagram:

SupportedPathsInPlace

Patterns & Practices SharePoint 2010 & 2007 Guidance Resources

Microsoft has released Patterns & Practices on SharePoint Guidance that are designed to help solution developers and architects make the right decisions and follow proven practices when building applications for SharePoint. The following are useful resources that you might want to explore:

Monday, February 14, 2011

SharePoint 2010 Official Site and Useful Blogs Worth Checking Out

A great starting place to understand more about SharePoint would be the MSDN SharePoint Developer Center:

You may also want to check out the MSDN Office Developer Center:

Also, Channel 9 offers a number of training kits that you can download and walk through, for both SharePoint and other Microsoft technologies. The Channel 9 Learning Center is located here:

With just these three sites, you’ll find a ton of developer resources.

Lastly, following are a few blogs worth checking out. You’ll certainly find more, but these have proven to be useful and informative over the years:

SharePoint 2007 Common Customizations and Recommendations When Upgrading to SharePoint 2010

Because of the downtime issues, in-place upgrades are better suited for small environments. In larger environments, the downtime needed to do an in-place upgrade may be too long. In-place upgrades may also not be suited for environments with significant custom code or other customizations. One of the benefits of an in-place upgrade is that the customizations in SharePoint 2007 are maintained. However, in order for your upgrade to be successful, your custom code must run in both SharePoint 2007 and SharePoint 2010.

Table below provides list of common customizations in SharePoint 2007 and recommendations on how to deal with them when upgrading to SharePoint 2010:

Customization

Good Choice

Better Choice

Custom Web Parts

Probably work out of the box with SharePoint 2010

Test on sample server, plan to rewrite for SharePoint 2010

Custom event handlers

Probably work out of the box with SharePoint 2010

Test on sample server, plan to rewrite for SharePoint 2010

Custom Site template

Create a site with the Custom Site template before upgrade

Recreate in SharePoint 2010, preferably as a Solution package and Feature

Custom site definition

Create UDF file for upgrade

Migrate to an out-of-the-box site template and deploy customizations as a Solution package and Feature

Customized (unghosted)
pages

Reset to site definition

Reset to site definition, and reapply customizations

Custom code or pages in /_layouts

Probably work out of the box with SharePoint 2010

Test on sample server, plan to rewrite for SharePoint 2010

Thursday, February 10, 2011

MSDN Virtual Lab: Introduction to Microsoft ASP.NET Web Forms 4.0

Today MSDN has released a newest Virtual Lab: Introduction to Microsoft ASP.NET Web Forms 4.0. Enjoy it!

Objectives

After completing this lab, you will be better able to:

  • Take advantage of the new ASP.NET Project Templates
  • Control server control ClientIds
  • Enable bi-directional routing support
  • Control the View State at application and page level

Scenario

ASP.NET 4 Web Forms provides enhancements in targeted areas and includes some new features. This Lab is also available online at http://channel9.msdn.com/learn and covers the following features:

New ASP.NET Project Templates

ASP.NET 4 Web Forms includes updated templates for creating new web applications and web sites. These templates include common functionality already implemented, which helps reducing the development time and provides guidance on best practices for building ASP.NET 4 Web applications.

Client IDs

Developers can now manage control IDs that affect rendered client IDs. The Control class now provides a new property called ClientIDMode that allows you to specify what behavior you want the runtime to take when determining whether or not to refactor the client ID upon rendering. This removes the previous bloat in the client ID.

URL Routing

ASP.NET 4 Web Forms introduces the new PageRouteHandler class that integrates URL Routing with Web Form Pages. URL routing in ASP.NET enables you to use URLs that do not have to map to specific files in a Web site. Because the URL does not have to map to a file, you can use URLs in a Web application that are descriptive of the user's action and are more easily understood by users and search engines. In URL routing, you define URL patterns that contain placeholders for values that are used when you handle URL requests. At run time, the pieces of the URL that follow the application name are parsed into discrete values, based on a URL pattern that you have defined.

View State

ASP.NET 4 Web Forms provides a more granular control over the View State. Developers can now disable the View State on a Page and enable it on specific server controls, and also disable it on a control an enable it on its childs.

Estimated time to complete

90 min

Computers used in this Lab

  • VS2010Labs-PC
  • The password for the Administrator account on all computers in this lab is: pass@word1

Tuesday, February 8, 2011

Getting SharePoint Web Application URL Using SPAlternateUrl

By using SPAlternateURL object, you can get the URL or SharePoint 2010 Web Application since the alternate access mappings are associated with a Web Application. Sample code is provided below:

public static string GetWebAppURL(SPWebApplication oWebApp, SPUrlZone urlZone)
{
    string retVal = string.Empty;
    try
    {
        foreach (SPAlternateUrl altUrl in oWebApp.AlternateUrls)
        {
            if (altUrl.UrlZone == urlZone)
            {
                retVal = altUrl.Uri.ToString();
                break;
            }
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
    return retVal;
}

public static string GetWebAppURL(SPSite oSite, SPUrlZone urlZone)
{
    string retVal = string.Empty;
    SPWebApplication oWebApp = null;
    try
    {
        if ((oWebApp = oSite.WebApplication) != null)
            retVal = GetWebAppURL(oWebApp, urlZone);
    }
    catch (Exception ex)
    {
        throw ex;
    }
    return retVal;
}

public static string GetWebAppURL(SPWeb oWeb, SPUrlZone urlZone)
{
    string retVal = string.Empty;
    SPWebApplication oWebApp = null;
    try
    {
        if(( oWebApp = oWeb.Site.WebApplication)!=null)
            retVal = GetWebAppURL(oWebApp, urlZone);
    }
    catch (Exception ex)
    {
        throw ex;
    }
    return retVal;
}

In order to call these functions, you need to pass either SPWebApplication, SPSite or SPWeb object and specify the SPUrlZone enumeration which has the following originating zone of a request:

  • Default - Specifies the default zone used for requests unless another zone is specified.
  • Intranet - Specifies an intranet zone.
  • Internet - Specifies an Internet zone.
  • Custom - Specifies a custom zone.
  • Extranet - Specifies an extranet zone.

Monday, February 7, 2011

How to Show Favicon In SharePoint 2010

Favicons are the little shortcut icons that appear in most modern browsers next to bookmarks, as well as in the address bar next to the site’s URL and on browser tabs - check out Wikipedia about Favicon.

SharePoint 2010 makes it very easy to add a favicon to custom master pages using the following code:

<SharePoint:SPShortcutIcon runat=”server” IconUrl=”/Style Library/Images/SPMalayaFavIcon.ico”/>

Favicon

Please note that when testing favicons Internet Explorer, if you are having trouble seeing a new favicon and you are certain that the code and the image are set up correctly, it may help to clear your browser cache, make a new bookmark, or even close and
reopen the browser.

Hiding the Name.dll ActiveX Control

If you are working on a public-facing Internet site, you should be aware that SharePoint may show a particularly annoying message at the top of Internet Explorer like shown below (This website wants to run the following add-on: ‘Name ActiveX Control’ from ‘Microsoft Corporation’: If you trust the website and the add-on and want to allow it to run, click here…)

NameDll

The message asks the user to run the Name.dll ActiveX Control add-on because the users don’t the SharePoint Server added to their trusted sites list. This control enables presence information to be displayed for authenticated users in SharePoint, and typically shows their availability in external Instant Messaging programs from inside SharePoint. Since anonymous users don’t really need this functionality you can turn off this message by using one of the following method:

  • In SharePoint 2010 the message can be turned off from Central Administration –> Manage Web Applications –> General Settings. Simply set Enable the Person Names Smart Tag and Online Status for Members to No. This will turn off the presence information and remove the ActiveX message for the entire web application.
  • Alternatively you can you can disable the message and functionality from a custom master page. Simply add the following code to your master page:

<script type=”text/javascript”>
    function ProcessImn(){}
    function ProcessImnMarkers(){}
</script>

This JavaScript code overrides the functions in SharePoint that cause this ActiveX message.

Sunday, February 6, 2011

SharePoint Guidance 2010 Hands On Lab Has Been Released

SharePoint Guidance 2010 Hands On Lab has just been released to CodePlex: http://spg.codeplex.com/ 

It consist of six labs:

  • Add and Remove Application Setting in Feature Receiver and Retrieve them in Web Part
  • Add and Remove Application Setting in Console Application and Use Hierarchy of them in Web Part
  • Add, Update, Remove, Search Application Setting in Sandbox
  • Read Application Setting in Sand Box Solution with ConfigProxy
  • Register SharePoint Logger in Feature Receiver and Use Logger in Web Part
  • Register Service in Feature Receiver and Display Service Instance in Web Part

Thursday, February 3, 2011

Random Number Generation - Not Random At All

using System;

namespace MyRandom
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 10; i++)
            {
                // Generate random number
                Random random = new Random();
                int myNum = random.Next(0, 10);
                // Write generated random number to console window
                Console.WriteLine("My random number: " + myNum.ToString());
            }
            // Pause running console window
            Console.ReadLine();
        }
    }
}

Did you know that if you declared Random Class within the loop as shown in the code sample above, it will not generate random number at all – in fact it will generate same number as shown in the screenshot below:

RandomNumber

To fix this problem, you need to declare Random Class outside of for loop as follows:

using System;

namespace MyRandom
{
    class Program
    {
        static void Main(string[] args)
        {
            Random random = new Random();
            for (int i = 0; i < 10; i++)
            {
                // Generate random number
                int myNum = random.Next(0, 10);
                // Write generated random number to console window
                Console.WriteLine("My random number: " + myNum.ToString());
            }
            // Pause running console window
            Console.ReadLine();
        }
    }
}

Windows Phone Training Kit Update Released

The Windows Phone Training Kit has been released today which includes few new labs – you can download Windows Phone 7 Training Kit for Developers here: RTM Refresh. The online training kit was also updated.

For more information, see: http://windowsteamblog.com/windows_phone/b/wpdev/archive/2011/02/02/windows-phone-training-kit-update.aspx

Using CrmDiscoveryService Web Service To Find Sales Org and Web Service End Point

Microsoft Dynamics CRM CrmDiscoveryService Web service provides a list of Web service endpoint URLs for each sales organization that you are a member of. The following sample code shows you how to use the Web service:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CRMDiscoveryServices;
using System.Configuration;
using System.Text;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            // Get CRM Dynamics credential from web configuration
            string crmUserId = ConfigurationManager.AppSettings["CRMUSERID"];
            string crmPassword = ConfigurationManager.AppSettings["CRMPASSWORD"];
            string crmDomain = ConfigurationManager.AppSettings["CRMDOMAIN"];

            // Create and configure the CrmDiscoveryService Web service proxy.
            CrmDiscoveryService discoveryService = new CrmDiscoveryService();
            discoveryService.Credentials = new System.Net.NetworkCredential(crmUserId, crmPassword, crmDomain);

            // Retrieve the list of organizations that the logged on user belongs to.
            RetrieveOrganizationsRequest orgRequest = new RetrieveOrganizationsRequest();
            RetrieveOrganizationsResponse orgResponse = (RetrieveOrganizationsResponse)discoveryService.Execute(orgRequest);

            // Locate the target organization in the list.
            if (orgResponse != null && orgResponse.OrganizationDetails.Length > 0)
            {
                StringBuilder sb = new StringBuilder();
                foreach (OrganizationDetail orgDetail in orgResponse.OrganizationDetails)
                {
                    sb.AppendLine("<B>Organisation name:</B> " + orgDetail.OrganizationName
                        + " <B>CRM Web application.:</B> " + orgDetail.WebApplicationUrl
                        + " <B>CrmService Web service URL:</B> " + orgDetail.CrmServiceUrl + "<BR/>");
                }
                Response.Write(sb.ToString());
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}

Please note that you need to add the CrmDiscoveryService web reference to your project. To add the web reference, select Add Service Reference, click Advanced, and then click Add Web Reference. In the URL field, paste the following string, replacing <server name> and <port number>:

http://<servername>:<port>/mscrmservices/2007/ad/crmdiscoveryservice.asmx?WSDL

Turn Off Warning Message On Unsupported Browsers in SharePoint 2010

Browser support in SharePoint 2010 has been greatly improved over SharePoint 2007, check out Technet’s Browser Support Matrix for SharePoint 2010. However Internet Explorer 6 (IE6) and other commonly used browsers are not browsers SharePoint 2010 supports from a content-authoring perspective. When you open a SharePoint page using IE6 browser, the following simple message will be displayed in the message box:

“Your Web browser will have problems displaying this web page. Changes to the sites may not function properly. For a better experience please update your browser to its latest version.”

WarningLegacyBrowser

Let say you are able to create a specific master page that works for anonymous IE6 users but how to turn off the warning message for all users with legacy unsupported browsers like IE6?

It’s simple! Find the following line at the bottom of the out-of-the-box master pages:

<SharePoint:WarnOnUnsupportedBrowsers runat=”server”/>

If you want to turn off the warning message for all users, you can simply remove it from master pages.

What Developers Must Know About HTML5 and IE9

Interesting article from MSDN: Internet Explorer 9 Beta Guide for Developers.

Wednesday, February 2, 2011

How To Turn Off Master Pages Applying to Application Pages

You may not want your custom branding to apply to the application pages, normally the way I do this is by not applying custom master page to SharePoint 2010 as the System Master Page. But wait, although this method prevents the application pages from showing the custom branding, but it also prevents the branding from showing on any nonpublising page too :(

I found a better way to do this that is by stop master pages from applying to application pages by turning off the feature completely from Central Administration, as follows:

  • Open Central Administration on the SharePoint server.
  • Under Application Management, click Manage web applications.
  • Select the desired web application from the list, and then from the ribbon, click General Settings.
  • In the dialog that appears, scroll down to Master Page Setting for Application _Layouts Pages and select No.
  • Scroll to the bottom of the dialog and click OK.

MasterPageSettingsForLayoutPage

    Now custom master pages for this entire web application will not apply to the application pages.

    Tuesday, February 1, 2011

    Delete a Corrupted List Using STSADM

    If you have a list that might appear to be in a corrupted state, when you try to delete it using SharePoint Web UI you will get the following error message:

    Invalid file name.
    The file name you specified could not be used.  It may be the name of an existing file or directory, or you may not have permission to access the file.<nativehr>0x81020030</nativehr><nativestack></nativestack>

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.Runtime.InteropServices.COMException: Invalid file name.
    The file name you specified could not be used.  It may be the name of an existing file or directory, or you may not have permission to access the file.<nativehr>0x81020030</nativehr><nativestack></nativestack>

    Source Error: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

    By using STSADM operation’s forcedeletelist, you can delete the corrupted list, here is the syntax:

    stsadm -o forcedeletelist -url <URL>

    Example:

    stsadm –o forcedeletelist –url http://sharepointserver/lists/yourcorruptedlist

    The Disposable in SharePoint Development

    If you don’t properly dispose of objects in the SharePoint object model that implement IDisposable, you will have memory usage problems in your application. The objects to be most careful of are SPSite and SPWeb, which must be disposed of because they consume large amounts of unmanaged memory.

    But Wait… I Thought Garbage Collection Took Care of Memory Management?

    The answer to this question is that an object like SPSite uses a mix of managed and unmanaged code. The memory usage of the managed side of SPSite is monitored by the .NET garbage collector, and when enough memory is used by the managed code, the garbage collector will kick in. The problem is that the .NET garbage collector doesn’t watch the unmanaged code’s use of memory and the unmanaged memory use is much greater than the managed memory use. So you can quickly run out of memory on the unmanaged
    side without .NET ever feeling like it needs to do a garbage collection.

    There are several coding patterns that I use when working with SPWeb and SPSite and other objects that implement IDisposable, so I would like to share with all of you:

    Using Dispose with SPSite or SPWeb  object

    SPSite oSite = new SPSite("http://localhost");

    // Do something

    SPWeb oWeb = oSite.OpenWeb();

    // Do something

    oWeb.Dispose();
    oSite.Dispose();

    The using clause with SPSite or SPWeb

    using (SPSite oSite = new SPSite("http://localhost"))
    {
        // Do something
        using (SPWeb oWeb = oSite.OpenWeb())
        {
            // Do something
        }
    }

    Using Dispose in the finally block of try, catch, finally code

    SPSite oSite = null;
    SPWeb oWeb = null;
    try
    {
        if ((oSite = new SPSite("http://localhost")) != null)
        {
            oWeb = oSite.OpenWeb();
            // Do something
        }
    }
    catch (Exception ex)
    {
        // Handle exception
    }
    finally
    {
        if (oWeb != null)
            oWeb.Dispose();

        if (oSite != null)
            oSite.Dispose();
    }

    Using Dispose with an object in a foreach loop

    using (SPSite oSite = new SPSite("http://localhost"))
    {
        foreach (SPWeb oWeb in oSite.AllWebs)
        {
            try
            {
                // Do something here
            }
            catch (Exception ex)
            {
                // Handle exception
            }
            finally
            {
                if (oWeb != null)
                    oWeb.Dispose();
            }
        }
    }

    Do Not Dispose of the SPSite and SPWeb Return By SPContext

    According to SharePoint best practices, SPSite and SPWeb objects returned by SPContext.Site, SPContext.Current.Site, SPContext
    .Web, and SPContext.Current.Web should not be explicitly disposed by user code.

    SharePoint Dispose Checker Tool

    Alternatively, you can use SPDisposeCheck to measure your code against known Microsoft dispose best practices.