SharePoint Dragons

Nikander & Margriet on SharePoint

How to check if the current user has already created a list item of a certain content type?

This probably would have been a bit of a hassle before SPMetal existed, right now it’s quite easy to do. Here’s what we didi:

  • We created a custom list.
  • In advanced settings, we’ve enabled content types and added the ootb content type TimeCard.
  • Then, we added two list items: one of the default type, and one of the TimeCard type.

Having our test list set up and ready to go, we’ve used SPMetal to create entities representing, among other, this custom list. We did this by issuing the following command at the command prompt (assuming 14/bin is in your environment path variables):

spmetal /web:http://astro /code Astro.cs

Add the generated Astro.cs file to a VS.NET SharePoint project (in this case, we’ve used a simple console application).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            // Open entities context generated by spmetal
            // if a sharepoint context is available, use new AstroDataContext(SPContext.Current.Web.Url) instead.
            using (var astro = new AstroDataContext(“http://astro”))
            {
                var timeItems = (from item in astro.MyMetalList
                                                                        where item.GetType() == typeof(Timecard)
                                                                        select (Timecard) item);

                // Enter specific user name in lambda expression below, for example using: SPContext.Current.Web.CurrentUser.LoginName
                int occurrences = timeItems.Where(item => item.UserName.ToLower() == “loisandclark\\administrator”).Count();
                if (occurrences == 0)
                {
                    Console.WriteLine(“it’s ok to add a request”);
                }
                else if (occurrences == 1)
                {
                    Console.WriteLine(“a request has already been submitted”);
                }
                else
                {
                    Console.WriteLine(“There are duplicate requests. This is an error, contact the administrator”);
                }

                foreach (var item in timeItems)
                {
                    Console.WriteLine(item.Title);
                }
            }
        }
    }
}

2 responses to “How to check if the current user has already created a list item of a certain content type?

  1. Pingback: Convert SPMetal Linq query to CAML « SharePoint Dragons

  2. Pingback: Print friendly list items « SharePoint Dragons

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: