Tuesday, 25 November 2008

DoesUserHavePermissions() Bug

I was working on a SharePoint Web Part to display listitems in a list that the user had access to.

I used the DoesUserHavePermission() function in the SPPermissionCollection class.
Soon I realized that the method had a bug.
It returned true if the user had permissions to the item and thats great, but when the user didnt had permissions to the item, it threw an AccessDeniedException.

To make this work i had to do two things:
1. the CatchAccessDeniedException property of the SPSites class must be set to false
2. catch the System.UnauthorizedAccessException

Wednesday, 29 October 2008

Number of Blog Comments

The problem was to get the Number of Blog Comments to a blog in Sharepoint.

It is possible to get the Number this way where item is an SPListItem:

string numberOfComments = item["NumComments"].ToString();

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...
}
}

Thursday, 10 July 2008

Preferred Links

This is a post with som links that I find usefull.

WSPBuilder
A SharePoint Solution Package (WSP) creation tool for WSS 3.0 & MOSS 2007.
A must for a SharePoint Developer!
http://www.codeplex.com/wspbuilder

Creating a Web Part with Custom Properties:
http://msdn.microsoft.com/en-us/library/ms948927.aspx

Ontolica
Search add-on for MOSS 2007
http://www.ontolica.com/

SharePoint Dispose Checker Tool
SPDisposeCheck is a tool to help you to check your assemblies that use the SharePoint API so that you can build better code. It provides assistance in correctly disposing of certain SharePoint objects to help you follow published best practice. This tool may not show all memory leaks in your code. Further investigation is advised if you continue to experience issues.
http://code.msdn.microsoft.com/SPDisposeCheck

ElBlanco's SharePoint Add-ons
A collection of add-ons for WSS 3.0 and MOSS 2007
The BlancoWorld SharePoint add-ons consist of:
  1. The BlancoWorld Event Receivers Manager. This add-on adds a link to list settings pages to allow the user to display, edit, add and delete event receivers to / from the list.
  2. The BlancoWorld Workflow Reporting Add-on - allows a user with the manage list right to report on workflows within a site collection.
  3. The BlancoWorld Notification Framework - Beta.
http://elblanco.codeplex.com/

Tuesday, 8 July 2008

Compare two DateTime

This is how you can compare two DateTime:

DateTime dateTime1 = new DateTime(2007, 04, 20);
DateTime dateTime2 = new DateTime(2008, 01, 20);

if (DateTime.Compare(dateTime1, dateTime2) < 0)
{
Console.WriteLine("DateTime2 is larger than dateTime1");
}
else if (DateTime.Compare(dateTime1, dateTime2) == 0)
{
Console.WriteLine("Dates are equal");
}
else if (DateTime.Compare(dateTime1, dateTime2) > 0)
{
Console.WriteLine("DateTime1 is larger than dateTime2");
}

Get yesterday using datetime

Hvis du skal have fat i igår ved hjælp af DateTime, så kan du gøre det på følgende måde ved at anvende (-) foran antal dage der skal trækkes fra:

DateTime.Now.AddDays(-1) // Gives you yesterday.

How to find objects in Generics with List.Find() method

Looking for help on how to find objects in Generics with List.Find() method .... and ... take a look what I have found.
In the follow example, I created a simple class:

public class Person
{
private int _id;
private string _name;

public int ID { get{ return _id;} set{ _id = value;}}
public int Name { get{ return _name;} set{ _name= value;}}

public Person(int id, string name)
{
_id = id;
_name = name;
}
}

In the example, there's a simple class with two private attributes. Now we're going to create a typed List of this object and take advantage of the Find() method.

public void CreateAndSearchList()
{
// Create and fill the collection
List myList = new List();
myList.Add(new Person(1, "AndreySanches"));
myList.Add(new Person(2, "AlexandreTarifa"));
myList.Add(new Person(3, "EmersonFacunte"));

// Find a specific object
Person myLocatedObject = myList.Find(delegate(Person p) {return p.ID == 1; });
}

This is a fast way to find objects using Generics.

How to install a Template in MOSS 2007

Just resently I discovered that Microsoft have a lot of free templates for MOSS 2007.
They are located at http://technet.microsoft.com/en-us/windowsserver/sharepoint/bb407291.aspx.
So how do I install one of these template?

After reading a lot of blogs and stuff, I came up with this walkthrough:

SharePoint Services Administration
First of all, the Windows "SharePoint Services Administration" has to be started.

You can start it by selecting "Administration Tools" - Services
Find the service on the list, right click and select Start.

Installation
Copy the file of the template you will install
Ex. ApplicationTemplateCore.wsp

To the folder:
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN
(location depends on your setup)

In cmd type the following and press enter:
CD C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN
(location depends on your setup)

And type the following and press enter:
stsadm -o addsolution -filename ApplicationTemplateCore.wsp

In cmd type the following and press enter
stsadm -o deploysolution -immediate -allowgacdeployment -name applicationtemplatecore.wsp

Note: Additional attributes may be required based on your Windows SharePoint Services configuration.

Now the template is deployed and ready to be used.

Delete template
If you want to remove the template again, type the following in cmd and press enter:
stsadm -o deletesolution -name applicationtemplatecore.wsp

If you have startet a job, you can find it here and stop it:
Operations -> Timer job status

Notifications vs SMTP

Before you add a Workflow with ex. Notification, you have to make sure the SMTP is installed and running.

To do this follow this description:
  1. Open Start -> "Control Panel" -> "Add or Remove Programs".
  2. Click "Add/Remove Windows Components".
  3. In the "Components" list, mark "Application Server" and click Details.
  4. Mark "Internet Information Services (IIS)" and click Details.
  5. Check "SMTP Service" and click OK.
This installs the "SMTP Service".

Now you have to set some properties.
  1. Open Start -> "Administrative Tools" -> "Internet Information Services (IIS)" vices.
  2. Select the Access tab.
  3. Check the ""nonymous access" and click OK.
  4. Select the Security tab and add Operators if necessary.
Thats it!

Install and use Microsoft Loopback Adapter

I am using a Virtual PC with MOSS 2007 installed and sometimes i am working on the pc without having any network connection.
To make this work, you can install and configure the Microsoft Loopback Adapter.

Jose Barreto wrote about installation and configuration in his blog and its done like this:
  1. Go to Start, Control Panel, Add Hardware. Click Next.
  2. When the Is the hardware connected? dialog box appears, click Yes, I have already connected the hardware, and then click Next.
  3. In the Installed hardware list, click Add a new hardware device, and then click Next.
  4. In the What do you want the wizard to do? list, click Install the hardware that I manually select from a list (Advanced), and then click Next.
  5. In the Common hardware types list, click Network adapters, and then click Next.
  6. In the Manufacturer list, click Microsoft.
  7. In the Network Adapter list, click Microsoft Loopback Adapter, and then click Next twice.
  8. If a message about driver signing appears, click Continue Anyway.
  9. In the Completing the Add Hardware Wizard dialog box, click Finish, and then click OK.
  10. To configure the adapter settings, go to Start, Control Panel, Network Connections, Loopback Adapter Connection.
  11. Then click properties, click on "Internet Properties (TCP/IP)" and click Properties.I configured my Loopback adapter with a fixed IP of 10.255.255.1 with a 255.255.255.0 mask. I left the default gateway blank on that adapter, so that it uses the other (DHCP-enabled) adapter to get out.
Thats it!

Link to Jose Barreto's blog: http://blogs.technet.com/josebda/archive/2006/11/07/installing-rtm.aspx

Monday, 7 July 2008

Workflow - Get notified the day before "Due Date"

I wanted to find a solution where a person who has created a task, gets notified the day before "Due Date".

The solution was to create a workflow in the Sharepoint Designer that starts when a new Task is created, and has actions and conditions like this:

Step 1

  1. CONDITIONS: (none: leave blank)
  2. ACTIONS (use "Add Time to Date"): Add -1 days to Tasks:DueDate (Output to Variable: notifyDate)

Step 2
  1. CONDITIONS: If Variable: notifyDate equals (ignoring time) Today
  2. ACTIONS (use "Send an Email"): Email Tasks:Created By
Note the Tasks is the name of the tasklist where the Workflow is added.

Happy creating!