Tuesday, 23 September 2008

Get the TimeSpan between two DateTimes

The task was to calculate the time different between two dates.

After surfing arround on the internet once again I found that there is a TimeSpan structure. This represents a time interval.

TimeSpan is used to find the difference between two dates and are used this way:

TimeSpan newTimeSpan = DateTime.Now - inputDateTime;

There are many usable properties in TimeSpan:
newTimeSpan.TotalDays
newTimeSpan.TotalHours
newTimeSpan.TotalMinutes.

Read more about TimeSpan on MSDN
http://msdn.microsoft.com/en-us/library/system.timespan.aspx

Monday, 22 September 2008

How to convert DateTime

Here is a list of ways to convert DateTime.

DateTime.Now; - 9/17/2007 8:29:25 AM
DateTime.Now.ToString(); - 9/17/2007 8:29:25 AM
DateTime.Now.ToShortTimeString(); - 8:29 AM
DateTime.Now.ToShortDateString(); - 9/17/2007
DateTime.Now.ToLongTimeString(); - 8:29:25 AM
DateTime.Now.ToLongDateString(); - Monday, September 17, 2007

--------------------------------------------------------------------------------

DateTime.Now.ToString("d"); - 9/17/2007
DateTime.Now.ToString("D"); - Monday, September 17, 2007
DateTime.Now.ToString("f"); - Monday, September 17, 2007 8:29 AM
DateTime.Now.ToString("F"); - Monday, September 17, 2007 8:29:25 AM
DateTime.Now.ToString("g"); - 9/17/2007 8:29 AM
DateTime.Now.ToString("G"); - 9/17/2007 8:29:25 AM
DateTime.Now.ToString("m"); - September 17
DateTime.Now.ToString("r"); - Mon, 17 Sep 2007 08:29:25 GMT
DateTime.Now.ToString("s"); - 2007-09-17T08:29:25
DateTime.Now.ToString("t"); - 8:29 AM
DateTime.Now.ToString("T"); - 8:29:25 AM
DateTime.Now.ToString("u"); - 2007-09-17 08:29:25Z
DateTime.Now.ToString("U"); - Monday, September 17, 2007 1:29:25 PM
DateTime.Now.ToString("y"); - September, 2007
DateTime.Now.ToString("dddd, MMMM dd yyyy"); - Monday, September 17 2007
DateTime.Now.ToString("ddd, MMM d "'"yy"); - Mon, Sep 17 '07
DateTime.Now.ToString("dddd, MMMM dd"); - Monday, September 17
DateTime.Now.ToString("M/yy"); - 9/07
DateTime.Now.ToString("dd-MM-yy"); - 17-09-07

How to get SPItem's "Created by" SPUser

I wanted to get the user who created a certain item and I found a simple sulution.

// Get the 'Created By' string.
string userValue = currentSPItem["Created By"].ToString();

// Get index of ';'
int index = userValue.IndexOf(';');

// Get id which is number before ';', so Substring from 0 to index
int id = Int32.Parse(userValue.Substring(0, index));

// Get the current user
SPUser currentSPUser = currentSPWeb.SiteUsers.GetByID(id);

Is the current SPUser a colleague?

I had an SPItem in a list, and I wanted to know: Is the creater of the item a colleague with the current user?

After surfing around on the internet, I came to a solution where I use UserProfile's.
I can get the loggedIn- and Current-UserProfiles Guid and check whether they are colleagues.

// Get Site and Web.
SPSite currentSPSite = SPControl.GetContextSite(Control.Context);
SPWeb currentSPWeb = currentSPSite.RootWeb;

// Loop through SiteUserInfoList
foreach (SPItem currentSPItem in currentSPWeb.SiteUserInfoList.Items)
{
string createdBy = currentSPItem["Created By"].ToString();
int index = createdBy.IndexOf(';');
int id = Int32.Parse(userValue.Substring(0, index));
SPUser currentSPUser = currentSPWeb.SiteUsers.GetByID(id);

// ServerContext provides run-time methods for shared services in Microsoft Office SharePoint Server 2007.
ServerContext serverContext = ServerContext.GetContext(currentSPSite);
// UserProfileManager is a collection of UserProfile objects.
UserProfileManager userProfileManager = new UserProfileManager(serverContext);

// The current UserProfile for the current item
UserProfile currentUserProfile = userProfileManager.GetUserProfile(currentSPUser.LoginName);
// The current UserProfile for the logged in user
UserProfile loggedInUserProfile = userProfileManager.GetUserProfile(currentSPWeb.CurrentUser.LoginName);

// Is the currentUserProfile a colleague with the loggedInUserProfile
if (currentUserProfile.Colleagues.IsColleague(loggedInUserProfile.ID))
{
Code here...
}
}