Thursday, June 5, 2008

Ask Questions

You can ask your questions and queries as a comment to this post. We will try to reply in its comment and so on.


Update: we stop receiving question as we have a lot question pending with answer.
we will reopen ask question soon.

Thanks,
SharepointKings Team

Difference between Synchronous and Asynchronous Events

The primary differences between Sync & Async event handlers are :

1) Synch Eve Handlers will work before the event is completed while AEH will fire after the event is completed.
2) SEH are mostly used to stop the event from completion in order to validate few things. It means you can cancel the event using SEH while it is not possible to cancel the event from AEH.
3) SEH methods has their method names ending with -ing while AEH method names will end with -ed. e.g. ItemAdding, ItemUpdating are SEH while ItemAdded, ItemUpdated are AEH methods.
4) SEH can be used to Add/Modify the values of list fields while using AEH its not possible as it fires after the completion of event.

Wednesday, June 4, 2008

Free Web Parts for MOSS 2007

Hi All,

There is a good link where you can find a free webparts for MOSS2007 to work with.

Here are few links :

Link1

Link2

As i will find more links i will surely put here.

Finding CSS in MOSS

While developing custom webpart, we always wonder for which style sheet class to use.
And try to find out by checking view source of the default page.

Here is the link for
CSS Reference Chart for SharePoint 2007 (Microsoft Office SharePoint Server 2007 and Windows SharePoint Services v3) by Heather Solomon.

This is the wonderful post by Heather Solomon which helped me to solve my many GUI and CSS related problems.

Tuesday, June 3, 2008

Custom Action Button on List's Tool Bar, WSS 3.0

Display a Custom Button say “Load Data” on the Toolbar of the Sales List

Solution:
In a Feature say “CustomContextMenu” we declare a Custom Action.

In the elements.xml file at “\CustomContextMenu\elements.xml” you will need to add this snippet.

<CustomAction Id="CustomContextMenu"
RegistrationType="List"
RegistrationId="10031"
Location="ViewToolbar"
Sequence="239"
Title="Load Data" >
<UrlAction Url="~site/PageFilter.aspx?ListId={ListId}&ItemId={ItemId}&SiteURL={SiteUrl}&Action=7"/>
</CustomAction>

Parameters values:

RegistrationType="List"

RegistrationId is the “Type” id found in the elementManifest.xml of the List on which you want to show the button. Here in our case if you open the elementManifest.xml of Sales List.
You will find that “Type” for this list is “10031”

Location="ViewToolbar". We want the Button to appear on the Tool Bar of the List.

In UrlAction mention the url where you want the control to navigate when you press the button.

Redirection from event handler

Scenario:
recently we have a requirement, for that we need to change default behavior of SharePoint.

Here is the requirement.
• While adding Item in the list, create sub site regarding that item.
• Redirect to the newly created site.
• Same case while updating item also.

Default behavior.
• After adding or updating item your SharePoint will redirect you to the page from where you come.
• Like in list if you adding item by clicking “New Item” from “alltems.aspx” SharePoint will redirect you back to the same page “allitems.aspx”.

How to do it?• Create an event receiver like ItemAdding and ItemUpdating for that List or library.
• In that event receiver, provision site programmatically.
• After provisioning new site, redirect user to newly created sub site using SPUtility.Redirect method. (need to include Microsoft.SharePoint.Utilities)

Roadblocks
• My first road block is that HttpContext is not available in event handler.
• To resolve this problem Check this
• Now after finding a way to get HttpContext we can use SPUtility.Redirect method but if we use it item is not added and your thread will redirect.
• And you’re other events (Asynchronous) like ItemAdded and ItemUpdated will not fire.

Solution
• To resolve this problem please check Code snippet below
public override void ItemAdding(SPItemEventProperties properties)
{

//perform validation if required.

// get the list which item to be added
SPSite objsite = new SPSite (properties.SiteId);
SPWeb objweb = objsite.OpenWeb (properties.RelativeWebUrl);
SPList objlist = objweb.Lists[properties.ListId];

//use this method to disable reoccurence of events.

DisableEventFiring();
SPListItem itemToAdd = objlist.Items.Add();

//add item to list

itemToAdd.Update();
EnableEventFiring();

// provision sub site and perform required action

//redirect it to your new destination like newly provisioned sub site or any other page you want.

SPUtility.Redirect(strNewresiractionUrl, SPRedirectFlags.Trusted,current);

}

I hope you got my problem and solution. With the help of this you can find your solution according to your requirement.

And thank you very much to Eric Bartels for his superb post which was helpful to resolve this problem.

Sharepoint People Picker and People Search

Hi All,

Here i would like to share something very interesting.

We have two options available in MOSS 2007 where we can find people. One is People Search and other is People Picker.

I would like to highlight the differene between these two. When you see the results you can see difference in them. the reason being difference is that they both use different data sources.

In the case of People Search it is fully reliable on Sharepoint profile database where as People Picker is fully reliable on Active Directory of your Domain. That is the reason it may return you more results.

The reason behind multiple results are following:

1) It also fetches people whoes account is disabled in AD
2) whose e-mail address is not mentioned
3) Service accounts are also countable in results

Let's have one understanding. People can have multiple account in multiple domains. But to keep in mind that there can be only one "Main" Account. Now when we say a Main account, it means that the account in which e-mail address is mentioned is called a "Main" account. and there can be only one "Main" account. All rest accounts do not have e-mail addresses.

So to filter out all those accounts that are not having e-mail address associated with it OR accounts that are disabled, here is a trick to accomplish this with the help of stsadm command.

After executing this command, in your people picker you will not get those accounts which are not having e-mail associated with it and also which are disabled.

Stsadm –o setproperty
–pn peoplepicker-searchadcustomfilter
–pv -<LDAP filter>
-url <WebApplication url>


For example :

Stsadm –o setproperty
–pn peoplepicker-searchadcustomfilter
-pv “(|(&(mail=*)(!(userAccountControl:1.2.840.113556.1.4.803:=2)))(objectcategory=group))”
-url http://myportal

But you have to make sure that you will have this option available only if you run Security Patch kb934525.

More details about this security patch can be found : here

That's it. your job is done.

Monday, June 2, 2008

People Picker is not able to find users from multiple domain

Hi All,

When you install MOSS 2007 in an environment where there are multiple domain, then User Profile import option only works with domain in which MOSS 2007 installed. It will not take users from other domain.

Let us say that you have one domain names "Domain1' where you have installed your MOSS2007 and when you use people picker inside MOSS, you will see that you are able to get the values for Domain1. Even if you have Domain2 also.

Here we need to make a new import connection for the Domain2 and from there we need to import user profile.

You need to follow following steps to accomplish this:

(1) You need to open SSP(Shared Service provider)

(2) Click on User Profiles and Properties

(3) Click on View Import Connections

(4) Create New Connection

(5) Add the following entries , also shown in figure below


Domain Name :Domain2
Select auto discover domain controller option (as radio button)
Search Base : DC=Domain2 (If you have multiple domain then seperate with comma(,)
Start Full Import option.

That's it. Your job is done. Now you will also get people from other domain as well in your people picker in MOSS2007.

Adding local file to site's folder

Hi All,

Here is a way how you can add your local files to SPFolder. All you need to do is just read the local file as a streams and then just called Files.Add Method to add a file in the Folder.

SPWeb objWeb = SPContext.Current.Web;
objWeb.AllowUnsafeUpdates = true;
SPFolderCollection objFolderColl = objWeb.Folders;
foreach (SPFolder objFolder in objFolderColl)
{
if (objFolder.Name == "Groups Document")
{
FileStream fStream = File.OpenRead("C:\\groups.txt");
byte[] contents = new byte[fStream.Length];
fStream.Read(contents, 0, (int)fStream.Length);
fStream.Close();

objFolder.Files.Add("Testdoc.doc", contents);
objFolder.Update();

break;
}
}

objWeb.AllowUnsafeUpdates = false;

That's it. Your job is done.

Using SPSiteDataQuery class to execute cross site queries

Hi All,

SPSiteDataQuery Class can be used to query several lists and several document libraries in different sites or from same site in web application.


May times we require to query against lists and query against more than one document libraries of same site or even from different sites. At that point of time we can use this object.
As name implies it’s really a SiteDataQuery, query that can be performed at site levels.


Interesting feature is that SPQuery returns result as SPListItemCollection, SPSiteDataQuery returns result as DataTable which is very useful.


This example performs a query against all document libraries in a particular Web site and returns the file paths for the items whose ID is greater than 1.


SPWeb oWebsite = SPContext.Current.Web;
SPSiteDataQuery oQuery = new SPSiteDataQuery();
string strWhere = "<Where><Gt>" +
"<FieldRef Name=\"ID\" />" +
"<Value Type=\"Number\">1</Value>" +
"</Gt></Where>";

string strOrderBy = "<OrderBy><FieldRef Name=\"FileRef\" /></OrderBy>";

oQuery.Query = strWhere + strOrderBy;

oQuery.Lists = "<Lists ServerTemplate=\"101\" />";
oQuery.ViewFields = "<FieldRef Name=\"Title\" />";

DataTable dtResults = oWebsite.GetSiteData(oQuery);


Make a point that by default limit of Lists that you can include in query is 1000 Lists. (Which is more than enough I think).

There is a good article available to read further about SPSiteQuery.
Click Here

For MSDN Link : Click Here



Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com