Tuesday, October 30, 2007

Simulate Parent / Child relationship in SharePoint 2007 with Folders & Content Types

One can create Parent / Child (A.K.A. Master / Detail) relationships in SharePoint 2007 by using lookups to reference a parent list from a child list. I will discuss another way of creating a Parent / Child relationship by adding a content type inside a folder.
We will create a parent / child relationship between company and employees. Here are our sample entities:

Company Parent (CompanyId, CompanyName)

Employee Child (Name, EmployeeId, Branch)
The strategy will be to make the "Company Parent" entity a folder which contains "Employee Child" items. To accomplish this we will create two new content types for each entity.

The "Company Parent" content type:

1) Site Actions ==> Site Settings ==> Site Content Types
2) Create

• Name: Company Parent

• Select parent content type from: Folder Content Types

• Parent Content Type: Folder

• Existing group: Custom Content Types

• Click OK
3) Click on "Add from new site column" on the "Site Content Type: …" page under column.
• Column name: CompanyId of type Number

• Click OK



NOTE: We will reuse the "Title" field to be the CompanyName.

The "Employee Child" content type:

1) Site Actions ==> Site Settings ==> Site Content Types

2) Create
• Name: Employee Child

• Select parent content type from: List Content Types

• Parent Content Type: Item

• Existing group: Custom Content Types

• Click OK
3) Click on "Add from new site column" on the "Site Content Type: …" page under column.
• Column name: EmployeeId of type Number

• Click OK
4) Click on "Add from new site column" on the "Site Content Type: …" page under column.

• Column name: Branch of type "Single line of text"

• Click OK


NOTE: We will reuse the "Title" field to be the EmployeeName.
Now we can go about our normal business of creating a custom list and using our two newly created content types "Company Parent" and "Employee Child".
1) Site Settings ==> Create

2) Click on "Custom List" under the "Custom Lists" column.
• Name: CompanyEmployees

• Click OK
3) Settings ==-> List Settings

4) Click on "Advanced Settings" under the "General Settings" column

5) At the very top, change Allow management of content types?" to Yes.

6) Click on OK

7) Under "Content Types" click on "Add from existing site content types".

8) On the "Add Content Types:" page
• Select site content types from: Custom Content Types

• Add "Company Parent" and "Employee Child" content types.

• Click on OK
Let’s add some dummy data.

1. Click on the "CompanyEmployees" list

2. Click on the little arrow beside New. You should see this:



3. Select "Company Parent"

4. Add the following bogus company data:
• Name: ACME Corporation; CompanyId: 123

• Name: Bogus Enterprises: CompanyId: 456
5. Add bogus company employees to "ACME Corporation"
• Click on "ACME Corporation". This causes the folder to open.

• Add the following employees to the ACME Corporation by clicking on New ==-> Employee Child:

• Title: Jane Doe; EmployeeId: 111; Branch: Downtown

• Title: James White; EmployeeId: 222; Branch: Airport

• Click on "Bogus Enterprises". This causes the folder to open.

• Add the following employees to Bogus Enterprises by clicking on New ==-> Employee Child:

• Title: Judy Black; EmployeeId: 333; Branch: Main Street

• Title: Brandon Wiley; EmployeeId: 333; Branch: Riverside

• Add another employee to ACME:

This concludes configuration and data entry. Let us now use the SharePoint 2007 Object Model to get at this data.
using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.SharePoint;

public class CompanyEmployees {

static void Main(string[] args) {

SPSite site = new SPSite("http://srvr/");

SPWeb web = site.AllWebs["/"];

SPList list = web.Lists["CompanyEmployees"];

foreach (SPListItem order in list.Folders) {

Console.WriteLine("Company ID : {0}", order["CompanyId"]);

Console.WriteLine("Company Name : {0}", order["Title"]);

SPQuery empItemsQuery = new SPQuery();

empItemsQuery.Folder = order.Folder;

SPListItemCollection empItems = list.GetItems(empItemsQuery);

foreach (SPListItem empItem in empItems) {

Console.WriteLine("\tEmployee ID {0}: {1} {2}",

empItem["EmployeeId"],

empItem["Title"],

empItem["Branch"]);

}

}

}

}

The above application will produce the following result:
Company ID : 123

Company Name : ACME Corporation

Employee ID 111: Jane Doe Downtown

Employee ID 222: James White Airport

Company ID : 456

Company Name : Bogus Enterprises

Employee ID 333: Judy Black Main Street

Employee ID 444: Brandon Wiley Riverside

Saturday, October 27, 2007

COMP-1536 & ACIT-1620 Fall 2007 week 09

Medhat Elmasry will be away attending a training course in downtown Vancouver. Here are some important notes on week 09:
 
Notes for COMP-1536 students

 
  • Jason Harrison will be delivering the lectures on Tuesday October 30, 2007 at 10:30 AM and Friday November 2, 2007 at 12:30 PM. He will be introducing you to PHP.
  • Medhat will not be present at the SET 1A lab on Monday October 29, 2007 at 1:30 PM. Students are expected to come to the lab session and use the time to work on assignment 3.
  • Sets 1B, 1C, 1D, and 1E must attend labs with Jason Harrison as usual.
  • You can post any questions as comments on my blog.

 
Notes for ACIT-1620 students

 
  • There will be no lecture on Monday October 29, 2007 at 10:30 AM. Instead, Medhat will makeup this lecture in the labs during the following week.
  • Brian Pidcock will be delivering the lecture on Friday November 2, 2007 at 1:30 PM on the topic of JavaScript functions.
  • Medhat will not be present at SET 1A and SET 1B labs on Monday October 29, 2007 at 3:30 PM and Tuesday October 30, 2007 at 8:30 AM respectively. Students are expected to come to the lab session and use the time to work on assignment 3.
  • You can post any questions as comments on my blog

Thursday, October 18, 2007

Creating "Calculated Field" columns in SharePoint 2007

You can pretty much use the same VBA syntax for calculated fields as in Excel. Here is a site that provides many examples of common formulas that can be used in SharePoint when defining "Calculated Field" columns.
Here is a formula that I created at work:
=IF(YEAR([Due Date])=1899,"No Due Date",TEXT([Due Date],"MM/dd/yyyy"))
Blank dates in SharePoint are assigned a date in year 1899. The above formula displays a message "No Due Date" for invalid dates or the valid date using format MM/dd/yyyy.

Sunday, October 14, 2007

Speaking at DevTeach in late November 2007

I am scheduled to speak at the DevTeach conference in Vancouver on Wednesday, November 28, 2007 from 11:00 AM to 12:15 PM. The topic I will be presenting is the same topic I gave to the .netBC group on Wednesday September 12, 2007. That presention took 2 hours. Therefore, I must shave off about 45 minutes from that presentation.

The DevTeach conference is moving outside of Montreal for the first time. I attended it twice in spring 2005 and spring 2007. It is definately a very worthwhile conference. If you want to be on top of current and up-coming technologies in our very fast moving profession, it is a good use of three days.
Jean-Rene Roy, who is the owner of the DevTeach & SQLTeach shows, is organizing a very different type of conference in Vancouver. New to Vancouver will be an XNA track, a Halo-3 competition, and a free beer party with Palermo. It looks like it is going to be a lot of fun. Here is more on the various activities:
XNA contest: http://www.devteach.com/contest.aspx
Party with Palermo! Free beer: http://www.partywithpalermo.com/
For the first time a XNA track at a independent conference: http://www.devteach.com/Session.aspx#79
Three very good Pre-con Post-Con:

http://www.devteach.com/PreConference.aspx

http://www.devteach.com/PostConference.aspx
Key note by Ken Levy on VSX: http://www.devteach.com/Keynote.aspx

How to hide "View All Site Content" link for anonymous users in SharePoint WSS 3.0?

This solution requires you to install Microsoft Office SharePoint Designer.
  • Go to "Site Actions" ==> "Site Settings"
  • Under Galleries, click on "Master Pages"
  • Select default.master and select "Edit in Microsoft Office SharePoint Designer".
  • In "Design" view locate "View All Site Content" on the left-hand-site navigation bar and click on it.
  • Go to "Code" view. You will see the following code highlighted: 
<Sharepoint:SPSecurityTrimmedControl runat="server" PermissionsString="ViewFormPages">

<div class="ms-quicklaunchheader">

<SharePoint:SPLinkButton id="idNavLinkViewAll" runat="server" NavigateUrl="~site/_layouts/viewlsts.aspx" Text="<%$Resources:wss,quiklnch_allcontent%>" AccessKey="<%$Resources:wss,quiklnch_allcontent_AK%>"/>

</div>

</SharePoint:SPSecurityTrimmedControl>
  • Change the PermissionString attribute value of the Sharepoint:SPSecurityTrimmedControl XML element from ViewFormPages to BrowseDirectories.
  • Save the default.master. If you login as an anonymous user you will not see the "View All Site Content" link. However when authenticated you will see this option.
NOTE: The "BrowseDirectories" permission is common to both the "Members" and "Owners" group permission levels, but is not set for the "Limited Access" (anonymous users) permission level. See a complete list of the SPBasePermissions Enumeration here.

Tuesday, October 9, 2007

Adding command prompt option to Windows Explorer in Windows 2003 server

1. Navigate in your Registry to
HKEY_LOCAL_MACHINE/Software/Classes/Folder/Shell and create a key called "Command Prompt" without the quotes.
2. Set the default string to whatever text you want to appear in the right-click menu
3. Create a new key within your newly created command prompt named "command" and set the default string to:
Cmd.exe /k pushd %L
4. The changes should take place immediately. Right click a folder and your new menu item should appear.

Friday, October 5, 2007

Making a SharePoint custom column visible in an edit list form

  1. Go to the list you are interested in
  2. Click Settings --> List Settings
  3. Under General Settings --> Click Advance Settings
  4. Click "Yes" for "Allow management of content types?"
  5. Click "OK"
  6. Click the list you are interested in under "Content Types"
  7. Now Click "Add from existing site or list columns"
  8. Click "Add >" to select the columns you want
  9. Click "OK"
  10. Go to the list you are interested in
  11. Click Settings --> List Settings
  12. Under General Settings --> Click Advance Settings
  13. Click "No" for "Allow management of content types?"

Thursday, October 4, 2007

How to get list and view GUIDs in WSS 3.0 or MOSS 2007?


List GUID

View GUID

Wednesday, October 3, 2007

Finding out the SharePoint list ID (ListTemplateId)

SharePoint 2007 offers an enhanced ability to catch certain user events such as ItemDeleting (before the fact) and ItemDeleted (after the fact). The API targets specific lists identified by list IDs. Example:


List template type
Template ID
Custom List
100
Document Library
101
Survey
102
Links List
103
Announcement List
104
Contacts List
105
Events List
106
Task List
107
Discussion Board
108
Picture Library
109




You can find out the list ID of any other list as follows:


  • Go to your 12 hives FEATURES folder C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES
  • Find the folder for the list you are interested in and open it.
  • Open the ListTemplates folder
  • Open the XML file and read the value of the Type attribute.
  • This information can also be found in the ONET.XML file in the appropriate site template directory under C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\SiteTemplates