Wednesday, July 27, 2011

Power of Delegate Control- What/Why/How

I get a lot of questions from junior developer as to why use a delegate control and how do you implement a delegate control?

A delegate control is a control that will be rendered. The key thing here is that at the run time it finds all controls with the same id and then adds to tree the control with lowest seq number. The delegate control is mainly added to Master Page that is tied to your site. If you view the Master Page's code you will find several delegate controls as those are added by SP.

Why use a delegate control?
You will choose to use a delegate control if you want to add some code that is rendered on every single page in your site. The alternative way is you would add a user control containing your code to every single page on your site. If you create new page, then you have to remember to add this user control. This is not the best approach and for this reason you create a delegate control and add it to Master Page and then it is rendered on every page that uses this MasterPage. You can have more than 1 delegate control of the same ID, but the delegate control with the lowest seq will get added to the tree to render.

How do you create a delegate control?
There are 2 things involved in a delegate control.
- Master Page
- User Control created using Visual Studio.

Steps required to create DC
1. Create a new Empty SharePoint 2010 Project.
2. Map it to ControlTemplate Folder.
3. Right Click on ControlTemplate Folder and add a User Control.
4. Add your code in the User Control.
5. Right click on the project and add a module. This will create Elements.XML file
6. In the elements.xml add this code:





7. Delete the feature that got created and then add a new feature with a more meaningful name with information.
8. Open your MasterPage that is beeing used by your Site and add code like this

at top around the GlobalNavigation control.
9. Deploy your code and test it.

Where / How to store email address in SharePoint 2010 when using Claims

When you are using Claims based authentication method, the user is never added SharePoint SiteCollection unless the user has logged to the site for the 1st time. However, when a user logs into the site for the first time, its identifier is only stored in a UserList List.

If you have a need to send emails to user as per of workflow or alert or anything, you need to find a place to store them. You could easily use the UserList to store this information. As a user is entering your site, you could write code to query the claims to extract email address the user primarily has and then extract that value and store it to the User List.

The location of this list http://myserver/_catalogs/user/Simple.aspx

Specail Consideration with this list:
- User List is very notorious as to when it updates the data.
- Initially when you create a new Site Collection, this list shows less than 15 columns and then a timer job runs and it adds around 40 more columns to the list.
- This list can only be seen by Site Collection Admin and above.
- In order to update to this list, you should ue run with elevated privs.

I will post some sample code soon.

Monday, July 25, 2011

Auto Increment Columns in SharePoint 2010/MOSS

As more and more applications are using SharePoint to store data and they want it to work simillar to SQL. How do you create auto increment columns?

The process is pretty cool. It would require sing SPD workflow.

Here are the steps:
-Add another column to list (Note: You cannot use ID as that is the internal column)
-List Settings->Advanced Settings->Manage Content.
-From List Settings page, click Item Content Type, and then click new column name.
-Next, choose the option to hide the column in the form.
-Goto SPD and create a workflow with "Update Item" in the list action and then set the new column to a value with current item:ID .
-Check for errors->Publish - >Save
-Finally, don't forgot to check the workflow to be staretd automatically as this is not the default.

Demystify Title Column in SharePoint 2010 List/MOSS

When you create a new SP List, you see a default Title column. If you try to delete this column, you don't see the option to delete when you goto List Settings->Columns. Have you wondered why you cannot delete this column?

The answer is because when you create a custom list, it is created with "Item" content type. This "Item" content type has 1 field and it is required field.

How do you delete the Title column?

Goto List Settings->Advanced Setting -> Choose Manage Content Types.

Then, from List Settings-> Click Item Content Type and there you will get the optional to make changes to Title column!

Monday, July 18, 2011

Different Kinds of Pages in SharePoint 2010

Did you know there are 3 kinds of pages in SharePoint 2010

- WikiPages -> A new kind of page available in SharePoint 2010. Used primarily like a free for all wiki page for adding Tables etc. This may contain WebParts

- WebPart Pages -> Existed in SP2007 and SP2010 -> A more structured page with layouts/zones/webparts etc.

- Publishing Pages ->Specifically for publishing

In addition, there are Application pages that are physically stored on the server.

Thursday, July 14, 2011

How to get total number of users logged in a day?

There is no direct way of retrieving this information. You have to solution out to get this data. In this article, I have outlined what needs to be done in order to get this data.

Step 1:
- Create a basic delegate control and add it to your MasterPage. In the delegate control, all you do is write code like this to update the OOB SharePoint list that manages all users who have logged into the site. This list can be accessed from this URL

http:/Your Server/_catalogs/users/simple.aspx

========================================Code=========================================================

//You have to run this with elevated privilidges as the user would not have access

//to this list. The list can only be accessed by Admin.

SPSecurity.RunWithElevatedPrivileges(delegate()

{

try

{

using (SPSite oSiteCollection = new SPSite(SPContext.Current.Site.ID))

{

using (SPWeb oWeb = oSiteCollection.RootWeb)

{

oWeb.AllowUnsafeUpdates = true;

SPList myUserList = oWeb.SiteUserInfoList;

SPListItem myUserItem = myUserList.Items.GetItemById(myLoggedInUser.ID);

myUserItem["Name"] = myUser.FirstName + " " + myUser.LastName;

myUserItem["Work e-mail"] = myUser.EmailAddress;

myUserItem.Update();

}

}

}

catch (Exception e1)

{

SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("External", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, "SecurityWebPart.AddPersonalDetailsToSite-" + e1.Message, "");

}

});

========================================Code=========================================================

This code really does not do much, you can even just put the update statement. When you add this code, it will put the time stamp when the user last logged onto your site.

Step 2
Then, write a timer job to extract daily logged user information from the above list and store it in another List. The reason you have to do this is so as to maintain the history.

Step 3
Finally, you can present your information in a webpart or directly from this list.

Wednesday, July 13, 2011

Troubleshooting: The user does not exist error or is not unique

Today I was rebuilding my VM as a result of some issue with the old VM. An authenticated claims user when added to a SPGroup threw the following error:

The user does not exist or is not unique.0x81020054"} System.Exception {System.Runtime.InteropServices.COMException}

I banged my head for some time before found out the problem was in the setup of WebApp. In my case, the WebApp had 2 STS Providers so it was pointing to the wrong STS Provider, which was causing the user to be not added to the group. However, the user was an authenticated user who could be directly assigned access to a resource, but not added to the group. The user was added and viewable from this URL-> http://YOURSERVER/_catalogs/users/simple.aspx.

The above solution is only applicable if you are using Claims Based Authentication.

SharePoint 2010 Administration Toolkit - Really cool

This is a great tool kit especially for administrators... Love the reports that are generated..This not only gives the reports, but also includes the following:

•Security Configuration Wizard (SCW) manifests
•Load Testing Toolkit (LTK)
•User Profile Replication Service
•Content Management Interoperability Services (CMIS)

Here is the link to install the kit