Tuesday, May 12, 2009

Smartpart in SharePoint (MOSS) 2007 - Easy Web User Controls

The SmartPart is a generic webpart that can contain an ASP.NET user control. Nothing new you would say, but the SmartPart can give your user control access to the SharePoint object model.

Smart Part allows developers to code simple (or complex) Web User Controls (.ascx files) utilizing the full set of visual development tools in VS and then deploy the result to a SharePoint (MOSS) 2007 site by just configuring the url of the user controls.

Need for SmartPart and it's benefits

  • The SmartPart is a SharePoint Webpart that can host any ASP.NET user control. Creation webparts by using the VS.NET designer(user control) instead of coding everything by hand.
  • It complies with sharepoint object model since it's a type of web part.
  • Easy to deploy user controls in MOSS sites
  • Simple configuration
  • Supports AJAX and hence we can create AJAX enabled Smartpart eaily.

The following steps shows the steps to create and deploy the smartpart in MOSS site

Step 1: Create a SmartPart
TO create a smartpart, we need to create a library project using visual studio editor and add the following code. Add the reference to Microsoft.SharePoint.dll.

using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebPartPages;
namespace SmartpartSample
{
/// <summay>
/// Description for UserControlContainer.
/// </summay>

[DefaultProperty("Text"),
ToolboxData("<{0}:UserControlContainer runat=server>"),
XmlRoot(Namespace = "SmartpartSample")]
public class SmartpartSample : Microsoft.SharePoint.WebPartPages.WebPart
{
private const string defaultText = "";
private string _userControl = defaultText;
private Control _control = null;

[Browsable(true),
Category("User Control"),
DefaultValue(defaultText),
WebPartStorage(Storage.Personal),
FriendlyName("User Control (.ascx)"),
Description("Path to the User Control (.ascx)")]

public string UserControl
{
// This property has the URL of the user control to be displayed in the web part
get
{
return _userControl;
}
set
{
_userControl = value;
}
}
/// <summay>
/// This method gets the custom tool parts for this Web Part by overriding the
/// GetToolParts method of the WebPart base class. You must implement
/// custom tool parts in a separate class that derives from
/// Microsoft.SharePoint.WebPartPages.ToolPart.

///An array of references to ToolPart objects.
/// </summay>
public override ToolPart[] GetToolParts()
{
// These tool parts are to set the properities of the smart part
ToolPart[] toolparts = new ToolPart[2];
WebPartToolPart wptp = new WebPartToolPart();
CustomPropertyToolPart custom = new CustomPropertyToolPart();
toolparts[0] = custom;
toolparts[1] = wptp;
wptp.Expand(Microsoft.SharePoint.WebPartPages.WebPartToolPart.Categories.Appearance);
custom.Expand("User Control");
return toolparts;
}

/// <summay>
/// Load the UserControl
/// </summay>

protected override void CreateChildControls()
{
base.CreateChildControls();
try
{
if (_userControl != defaultText)
{
_control = this.Page.LoadControl(_userControl);
}
else
{
_control = new LiteralControl(string.Format("To link to content, open the tool pane and then type a URL in the Link text box.", 1, 129, this.ID));
}
}
catch (System.Exception ex)
{
_control = new LiteralControl(string.Format("Error: unable to load {0}
Details: {1}", _userControl, ex.Message));
}
if (_control != null)
{
// Add to the Controls collection to support postback
this.Controls.Add(_control);
}
}

/// <summay>
/// Render this Web Part to the output parameter specified.
/// </summay>

/// The HTML writer to write out to
protected override void RenderWebPart(HtmlTextWriter output)
{
EnsureChildControls();
if (_control != null)
{
_control.RenderControl(output);
}
}
}
}

Step 2: Adding the smart part into MOSS Site
1. Place the dll of the smart part project into the bin folder present in the virtual directory folder of the MOSS site.
2. Add a safe control entry for the smart part in the web.config file of the MOSS Site.

<SafeControl Assembly="Assembly Name of the SmartPart, Version=1.0.0.0, Culture=neutral" Namespace="Namespace of the SmartPart" TypeName="*" Safe="True" />

3. Modify the trust level to WSS_Medium in the web.config as shown below.
<trust level="Full" originUrl="" />

4. Open the SharePoint portal where the web parts have to be deployed.

5. Click on Site Actions->Site Settings->Modify All Site Settings

6. Click on Web parts option under Galleries

7. Click on New tab on the Web Part Gallery page.

8. Click on the checkboxes against the Smart part which we have deployed using the above mentioned steps.

9. Click the “Populate Gallery” button.

Step 3: Placing the User Controls and link to MOSS site

1. Copy the User controls to the UserControls folder in the Virtual directory of the MOSS Site.

2. If the UserControls folder is not available create the same.

Step 4: Using the smart in the site pages

1. Create a new page in the MOSS site and edit the page.

2. Add the Smart Part to the page and edit the properties of the smart part.

3. In the properties window of the smart part provide the user control path to be loaded in that page.

4. The path of the usercontrol file should be in this format “~\UserControls\UserControl File Name”.

5. Click on OK button, now the User control should be visible on the page.

6. Exit the page edit mode.

Tuesday, May 5, 2009

Frequently asked interview questions in MOSS 2007 - Part II

This article explains the frequently asked interview questions in MOSS 2007 (SharePoint) with answers. This covers basic and advanced concepts of MOSS

If you have not gone through the first part of the FAQs, click here (Part One)

1. What all elements of SharePoint to which Workflows can be applied?
While workflow associations are often created directly on lists and document libraries, a workflow association can also be created on a content type that exists within the Content Type Gallery for the current site or content types defined within a list. Workflows are applied at

  • At the level of a list (or document library)
  • At the level of a content type defined at site scope
  • At the level of a content type defined at list scope

2. What are the ways to initiate the workflow?
The following ways in which the workflows can be initiated

  • Automatic (without User Interface and no user inputs)
  • Manual (standard WSS UI interface - Out of the box user interfaces)
  • Manual (Custom UI Interface developed using .net or InfoPath forms)

3. What are the types of input forms that can be created for a workflow?
You can create four different types of input forms including

  • Association form
  • Initiation form
  • Modification form
  • Task edit form.
Create a Custom Workflow in MOSS with input forms is available here
Note that these forms are optional when you create a workflow template.

4. What are ways to create input forms for workflow?
Two different approaches can be used to develop custom input forms for a WSS workflow template.
  • You can create your forms by using custom application pages, which are standard .aspx pages deployed to run out of the _layouts directory. (The disadvantage with this approach is lot of code required when compared to InfoPath approach).
  • Using Microsoft Office InfoPath 2007 (The disadvantage with this approach is dependent on MOSS and it cannot run in a standalone WSS environment)


5. What is the difference between method activity and event activity in Workflow?

A method activity is one that performs an action, such as creating or updating a task.

An event activity is one that runs in response to an action occurring.

6. What does SPExport/SPImport class do?

This class is available in Microsoft.SharePoint.Deployment namespace.SPExport Supports export of specified content from a source Windows SharePoint Services site collection to a CAB file (with .cmp file extension or custom file extension) in XML format.

SPImport class Supports importing specified content into a Windows SharePoint Services site collection using a migration package (.cmp) file in XML format.Using these APIs, we can Export and Import

To learn more about these APIs, Click here


7. What are the defaults SharePoint permission groups available? How this is different from windows security groups?

Following SharePoint groups are provided by default.

  • Reader - Has read-only access to the Web site.
  • Contributor - Can add content to existing document libraries and lists.
  • Web Designer - Can create lists and document libraries and customize pages in the Web site.
  • Administrator - Has full control of the Web site.

The permissions can be applied at

  • Site collection level
  • Web site/ sub site level
  • Library or list levelList item or page level

We can add Windows user accounts and Windows security groups to your SharePoint groups for providing access.

8. What are the different types of search queries in SharePoint?

Search in Windows SharePoint Services supports three types of search syntax for building search queries:

  • Keyword Query syntax (search terms are passed directly to the Search service using OOB web parts like Search Web part)
  • SQL syntax (extension of SQL syntax for querying crawl databases by customizing the CoreResultsWebPart)
  • URL syntax (search parameters are encoded in URL, and posted directly to the search page E.g http://localhost/search/Results.aspx?q=keyword)

To learn more about search queries click here

9. What is the relationship between Microsoft SharePoint Portal Server and Microsoft Windows Services?

Microsoft SharePoint Products and Technologies (including SharePoint Portal Server and Windows SharePoint Services) deliver highly scalable collaboration solutions with flexible deployment and management tools. Windows SharePoint Services provides sites for team collaboration, while Share Point Portal Server connects these sites, people, and business processes—facilitating knowledge sharing and smart organizations. SharePoint Portal Server also extends the capabilities of Windows SharePoint Services by providing organizational and management tools for SharePoint sites, and by enabling teams to publish information to the entire organization.


10. What does SPWeb.EnsureUser method do?

Checks whether the specified login name belongs to a valid user of the Web site, and if the login name does not already exist, adds it to the Web site.

e.g

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

SPWeb myweb = site.OpenWeb("");

SPUser usr = myWeb.EnsureUser("username");


11. While creating a Webpart, which is the ideal location to Initialize my new controls?

Override the CreateChildControls method to include your new controls. To make sure that the new controls are initialized. call 'EnsureChildControls' in the webparts Render method. You can control the exact Rendering of your controls by calling the .Render method in the webparts Render method.


12. How to query from multiple lists ?

Use SPSiteDataQuery to fetch data from multiple lists.

13. What are User Defined functions in Excel Services Calculation?

User-defined functions (UDFs) are custom functions that extend the calculation and data-import capabilities of Excel. Developers create custom calculation packages to provide:

  • Functions that are not built into Excel.
  • Custom implementations to built-in functions.
  • Custom data feeds for legacy or unsupported data sources, and application-specific data flows.

To use custom functions in a class as an Excel Services UDF class, you must mark your UDF class and method with the Microsoft.Office.Excel.Server.Udf.UdfClass and Microsoft.Office.Excel.Server.Udf.UdfMethod attributes.UDF assemblies are disabled by default.To Enable UDF Assemblies,Each Excel Services trusted location in the Shared Services Provider (SSP) has an AllowUdfs flag to true.


14. What are the sources of User Profiles information in SharePoint?

User Profile information can be imported from Active Directory through master connection.

In addition to Active Directory, importing profile information from all of the following data sources using Business Data Catalogue (BDC)

  • LDAP directory (which is not Active Directory)
  • Databases such as SQL Server
  • Enterprise applications (like SAP or PeopleSoft)


15. What is the use of query.ViewAttributes OR how can you force SPQuery to return results from all the folders of the SharePoint list?

If you use SPQuery on any SPlist, it will bring back results from the current folder only (One level only).If you want to get results from all the folders in the list (including sub folders) then you need to specify the scope of the query by the use of ViewAttributes.

e.g. SPList oList = oWebsite.Lists["DocLib_Name"];

SPView oView = oList.Views["View_Name"];

SPQuery oQuery = new SPQuery(oView);

oQuery.ViewAttributes = "Scope=\"Recursive\"";




Friday, May 1, 2009

Frequently asked interview questions in MOSS 2007

This article explains the frequently asked interview questions in MOSS 2007 (SharePoint) with answers

1. What MOSS (SharePoint) offers?

  • A framework that allows building business sites rapidly.
  • Lots of Out of the box features, Workflows and site templates already built-in Excellent integration with Word, Excel, PowerPoint etc
  • Audience Targeting and Document collaboration
  • Enterprise Search for content and people search
  • Wikis and Blogs
  • Really Simple Syndication (RSS) support

2. Is SharePoint supports Globalization?

Yes. Install different language packs for SharePoint. To learn about installing language pack, click here

3. Does SharePoint search results supports audience filtering?

The default SharePoint search results based on

  • Security of the items
  • Input keyword

The default search doesn’t support filter based on audience targeting. But we can customize coreresultswebpart to do that. To learn more about customizing core results web part is here


4. What is Shared Service Provider (SSP) in MOSS?

In MOSS 2007 there is this new concept of Shared Services Providers (SSP). The idea being that there are certain services that really make sense to centrally manage and share. A good example is user profiles. With a SSP we can import all of the profile information from AD (thru master connection) once as well as BDC (Business Data Catalog) and then our various web applications can consume the data. So maybe we have http://mossapp1/ and http://mossapp2/ it doesn't make sense for each one to maintain identical profile information, they should share.

The major services of SSP

  • Profiles and Audiences
  • My Sites
  • Search configuration
  • Excel Services
  • BDC (Business Data Catalog)
  • Manage usage analytics

5. Some of Out of the box web parts and its usage?

Web Part is a modular unit of information that consists of a title bar, a frame, and content. Web Parts are the basic building blocks of a Web Part Page. Web Part Page is a special type of Web page that contains one or more Web Parts.

Some of the major web parts are

  • Content Editor Web Part to add formatted text, tables, hyperlinks, and images to a Web Part Page. The Content Editor Web Part is intended for adding HTML content to a Web Part Page.

  • Page Viewer Web Part is to display a Web page, file, or folder on a Web Part Page. You enter a hyperlink, file path, or folder name to link to the content. You can use the Page Viewer Web Part only in a browser that supports the HTML IFRAME element. Displaying a file or folder requires Microsoft Internet Explorer.

  • Content Query Web Part (CQW or CQWP) is to display SharePoint content from another source like SharePoint list, pages library on a SharePoint page.

  • A Summary Link Web Part and a Summary Link field control both provide an easy way to build a page of links to various resources, both inside and outside of your site. You can control the appearance, organization, and presentation of the links that you add to a Summary Link Web Part or field control.

  • A Table of Contents Web Part is a configurable component that you can add to a Web Part Page. You use the Table of Contents Web Part to automatically generate a site map that point to various parts of your Office SharePoint Server 2007 site collection. When you add a Table of Contents Web Part to a page, you specify which part of your site collection the Web Part should generate links to, how the links are presented, and how the links are organized.

  • My Inbox web part Shows Inbox, open e-mail messages and even forward, reply, etc.
    Search Core Results Web Part, one of the most important ones displaying the search results to the user.

  • Excel Web Access web part is a feature of MOSS enterprise Edition. This web part is used to display an excel file from a Trusted file location. This comes as part of Excel services.

  • A Key Performance Indicator (KPI) web part is a visual cue that communicates the amount of progress made toward a goal. This article explains how to create KPIs by using Microsoft Office SharePoint Server 2007 KPI lists and how to display KPIs on Web pages.

6. What are the Components of Excel Services in MOSS?

There are three major components of Excel services

  • Excel Web Access
  • Excel Web Services
  • Excel Calculation Services

7. Personalization in MOSS and it’s different models

Personalization means providing a user centered experience of the Portal. Keeping user settings and preferences same every time the user logs in.

There are three main features in MOSS 2007 personalization model.

User profiles allow you to search and connect with people within your organization based on information published about them. MOSS 2007 provides a new search scope for searching people. Index Server crawls the user profile store to get the user’s information.


Audience Targeting: MOSS 2007 allows you to target content to people according to their membership in a particular audience. It supports targeting to rules-based audiences, distribution lists, and Windows SharePoint Services groups. Except for Windows SharePoint Services groups, these audiences can span one or more portal sites in a deployment.
Using targeting, you can target content in the portal site for viewing by one or more specific audiences. By default, you can display targeted content on the home page and on personalization sites.


My Site is a collection of Profile pages, personal sites, and personalization sites created in the Office SharePoint Server 2007 site. The Profile page of the My Site displays your user profile information. Your personal site provides personalized and customized information. Office SharePoint Server 2007 also supports personalization sites. Personalization sites display targeted content to users based on their membership in a particular audience or by slicing data.


8. What is WSS?

Windows SharePoint Services (WSS) is a set of components and services that is considered part of the Windows Server 2003 operating system. However, licensing and support for WSS is controlled by the Windows platform team, not the Office team.

Microsoft Windows SharePoint Services 3.0 is a versatile technology that organizations and business units of all sizes can use to increase the efficiency of business processes and improve team productivity. With tools for collaboration that help people stay connected across organizational and geographic boundaries, Windows SharePoint Services gives people access to information they need.
Built on Microsoft Windows Server 2003, Windows SharePoint Services also provides a foundation platform for building Web-based business applications that can flex and scale easily to meet the changing and growing needs of your business. Robust administrative controls for managing storage and Web infrastructure give IT departments a cost-effective way to implement and manage a high-performance collaboration environment. With a familiar, Web-based interface and close integration with everyday tools including the Microsoft Office system, Windows SharePoint Services is easy to use and can be deployed rapidly.


9. What are the different Workflow Types in MOSS?

Windows Workflow Foundation supports two fundamental workflow styles. You can create workflows of either type for Windows SharePoint Services 3.0.

A sequential workflow represents a workflow as a procession of steps that execute in order until the last activity completes. However, sequential workflows are not purely sequential in their execution. Because they can receive external events, and include parallel logic flows, the exact order of activity execution can vary somewhat.

A state machine workflow represents a set of states, transitions, and actions. One state is denoted as the start state, and then, based on an event, a transition can be made to another state. The state machine can have a final state that determines the end of the workflow.

10. What are content types?

A content type is a flexible and reusable WSS type definition that defines the columns and behavior for an item in a list or a document in a document library.

For example, you can create a content type for a customer presentation document with a unique set of columns, an event handler, and its own document template. You can create a second content type for a customer proposal document with a different set of columns, a workflow, and a different document template.


11. What is the difference between Synchronous and Asynchronous events in MOSS?

Synchronous calls ending with 'ing' E.g. ItemDeleting

  • Occur before the event.
  • Event Handler code execute BEFORE action is committed
  • WSS waits for code to return
  • Option to cancel and return error code

Asynchronous calls ending with 'ed' E.g. ItemDeleted

  • Occur after the event.
  • Event Handler code executes AFTER action is committed
  • WSS does not wait for code to return
  • Executed in its own Worker thread.

Events in MOSS occurs at

  • Site Level - E.g SiteDeleted & SiteDeleting
  • List Level - E.g FieldAdded & FieldAdding
  • List Item Level - E.g ItemAdded & ItemAdding

12. What is ServerUpdate() ?

Any changes in the list, i.e. new addition or modification of an item, the operation is complete by calling the Update method. But if a List is set to maintain versions and you are editing an item, but don't want to save it as a new version, then use the SystemUpdate method instead and pass in 'false' as the parameter.

13. What does AllowUnsafeUpdates do ?

If your code modifies Windows SharePoint Services data in some way, you may need to allow unsafe updates on the Web site, without requiring a security validation. You can do by setting the AllowUnsafeUpdates property.


using(SPSite mySite = new SPSite("http://localhost")){

using(SPWeb myWeb = mySite.OpenWeb())

{myWeb.AllowUnsafeUpdates = true;

SPList interviewList = myWeb.Lists["listtoinsert"];

SPListItem newItem = interviewList.Items.Add();
newItem["test"] = "test";

newItem.Update();}}


14. What does RunWithElevatedPrivileges do?

Executes the specified method with Full Control rights even if the user does not otherwise have Full Control.

Assume that you have a Web Part in which you want to display information obtained through the Windows SharePoint Services object model, such as the name of the current site collection owner, usage statistics, or auditing information. These are examples of calls into the object model that require site-administration privileges. Your Web Part experiences an access-denied error if it attempts to obtain this information when the current user is not a site administrator. The request is initiated by a nonprivileged user. you can still successfully make these calls into the object model by calling the RunWithElevatedPrivileges method provided by the SPSecurity class.
SPSecurity.RunWithElevatedPrivileges(delegate() {

// Add custom code here

});


15.What is a SharePoint Feature? What files are used to define a feature?

A SharePoint Feature is a functional component that can be activated and deactivate at various scopes throughout a SharePoint instances, such as at the farm, site collection, web level etc.

Features have their own receiver architecture, which allow you to trap events such as when a feature is installing, uninstalling, activated, or deactivated. The element types that can be defined by a feature include menu commands, link commands, page templates, page instances, list definitions, list instances, event handlers, and workflows.

The two files that are used to define a feature are the feature.xml and manifest file (elements.xml).

  • The feature XML file defines the actual feature and will make SharePoint aware of the installed feature.
  • The manifest file contains details about the feature such as functionality.

Go to FAQs Part II