Archive

Posts Tagged ‘MOSS 2007’

Item Level Permission in workflow CreateTaskXXX activities

September 27th, 2007 11 comments

Both CreateTask and CreateTaskWithContentType  activities  have a property called ” SpecialPermissions ” that takes a hashtable of key-value pairs of type string and SPRoleType. Setting the SpecialPermissions property  in your code will strip out all existing permissions inherited from the parent list(Workflow Task List) and only adds permissions for each pair you added to the hashtable .

   private void createTask(object sender, EventArgs e)
{
…….
CreateTask task1 = sender as CreateTask;
HybridDictionary permsCollection = new HybridDictionary();
permsCollection.Add(taskProps.AssignedTo, SPRoleType.Administrator);
task1.SpecialPermissions = permsCollection;
}

Alternatively, If you are using “CreateTaskWithContentType” activity, you have one other option : The event handler bound to your content type. Here is how I’d do it:

1) I create the Task using “CreateTaskWithContentType” activity from my workflow. Obviously, I already have a content type in place.

2) I have also attached an event handler to the content type I use above. Oops! I forgot to say that there is no way you can visually do that (except this one), so I use a programmatic approach to assign an event handler to my content type when the feature that contains my content type get activated.

3) So I have to create a Feature that contains the content type (MyTaskContentType.xml is content type definition which I will skip for the sake of code brevity)

<?xml version=”1.0″ encoding=”utf-8″ ?>
<Feature Id=”5FED1202-C6B8-453e-9EC0-F328655ED4DC”
Title=”My Workflow Task Content Types”
Description=”….”
Version=”12.0.0.0″
Scope=”Site”
xmlns=”http://schemas.microsoft.com/sharepoint/
ReceiverAssembly=”DevHorizon.SharePoint.Workflows, Version=1.0.0.0, Culture=neutral, PublicKeyToken=207a12b7817b805c”
ReceiverClass=” “DevHorizon.SharePoint.Workflows.AddConentTypesEventHanlders”>
<ElementManifests>
<ElementManifest Location=”DivRepTask.xml” />
<ElementManifest Location=”HRAdminTask.xml” />
</ElementManifests>
</Feature>

4) I create a feature receiver class for this feature, I get a reference to the content type and attach the event handler to it programmatically:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
using (SPSite siteCollection = (SPSite) properties.Feature.Parent)
{
using (SPWeb web = siteCollection.OpenWeb())
{
System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly(); SPContentType ctMyTask = web.ContentTypes[“My Conent Type”];
ctMyTask.EventReceivers.Add(SPEventReceiverType.ItemAdded, a.FullName,”MyTaskItemEventReceiver”);
ctMyTask.Update();
web.Update();

}
}
}

5) Now,I need to create an event handler class named “MyTaskItemEventReceiver” that inherits from SPItemEventReceiver.Make sure you decorate this class with the right attributes to target to the featureId that contains your content type and the content type Id that you are writing this event handler for. You also need a unique GUID for your event handler

[TargetContentType(“feature id goes here”, “content type id goes here”)]
[Guid(“and here is the unique id for your enevnt handler”)]

6) In ItemAdded method of your event handler add the following code:

DisableEventFiring();
SPListItem listItem = properties.ListItem;
try
{
listItem.BreakRoleInheritance(false);
listItem.Update();
listItem =SetItemLevelPermissions(listItem.Web, listItem, SPRoleType.Contributor, listItem[“Assigned To”].ToString());
listItem.Update();
}
catch (Exception ex)
{
//Add error handling code here
}
EnableEventFiring();

7) And finally add this helper method somewhere accessible from within your event handler

public static SPListItem SetItemLevelPermissions(SPWeb setSPWeb, SPListItem setListItem, SPRoleType setRoleType, string assignedTo)
{
int index = assignedTo.IndexOf(‘;’);
int id = Int32.Parse(assignedTo.Substring(0, index));
SPUser user = setSPWeb.SiteUsers.GetByID(id);
SPRoleDefinition roleDefinition = setSPWeb.RoleDefinitions.GetByType(setRoleType);
SPRoleAssignment roleAssignment = new SPRoleAssignment(user.LoginName, string.Empty, string.Empty, string.Empty);
roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
setListItem.RoleAssignments.Add(roleAssignment);
return setListItem;
}

Now when your workflow creates a task, its item level permission will be broken and set to “assigned to” field.Happy ending!

Categories: MOSS 2007 Tags:

Programming under the influence

September 26th, 2007 2 comments

Let’s say you have a list (i.e.task list ) for which you need to capture ItemAdded event using an event handler and You have to do something unusual like the following:

[SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
public override void ItemAdded(SPItemEventProperties properties)

        {
            SPListItem testtask = properties.ListItem.ParentList.Items.Add();
            testtask[“Title”] = “You are fired!::” + DateTime.Now.ToString();
            testtask[“Due Date”] = DateTime.Now;
            testtask[“Priority”] = “(1) High”;
            testtask.Update();
        }

Well,You are trapped in a loop. How? Somebody adds  a task and your event handler fires and adds another task and  so on and so forths. I know this may sound stupid to do, by my point is something else here. Event handler OM is now smart enough not to let you get trapped in an infinite loop ,so recursion depth in such case is set to 10 (for itemAdded) and looping only occurs 10 times. It is really cool! Look at the picture below:

 If you really have to do such a thing then at least use DisableEventFiring() and EnableEventFiring(); to stop the event handler from firing 10 times. 

[SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
public override void ItemAdded(SPItemEventProperties properties)
    {
       DisableEventFiring();
       SPListItem testtask = properties.ListItem.ParentList.Items.Add();
       testtask[“Title”] = “You are fired!::” + + DateTime.Now.ToString();
       testtask[“Due Date”] = DateTime.Now;
       testtask[“Priority”] = “(1) High”;
       testtask.Update();
       EnableEventFiring();
     }

Categories: MOSS 2007 Tags:

Plan your three Must-Attend conferences in 2008

September 17th, 2007 No comments

1) Microsoft Office System Developer Conference 2008 | San Jose, California | Feb 10-13 2008
I was just informed that the official site for this has gone live. I think this is the first time ever this conference is open to the public, so everyone is invited. This is a fantastic opportunity for architects, developers, industry experts, and Microsoft insiders to come together to discuss various developing solutions in Office System.
2) SharePoint 2008 conference | Seattle,WA | March 3-6,2008 Take advantage of  the “early bird” special discount. Register Today
3) Tech·Ed | Orlando, FL | June 9-13, 2008
Be among the first to get conference information, session agendas, and preregistration details by signing in the Tech·Ed 2008 Guestbook.If you live in Greater Toronto Area, I’d highly recommend you to mark your calendar for 20th of October for Toronto SharePoint Camp. I can’t wait to see you all there! 

Categories: General, MOSS 2007 Tags:

The latest MOSS and WSS SDK release

August 24th, 2007 No comments

There has been a major update to the MOSS and WSS Software Development Kit (SDK) which makes this release an important one.Now there are more samples and articles included and a series of new tools and features are nicely packaged up in the download files.You can install them from the following locations:

MOSS 2007 SDK 1.2:  Includes Conceptual and Class Library Reference documentation, Web Services documentation, and Developer Tools and Samples for MOSS and WSS. http://www.microsoft.com/downloads/details.aspx?FamilyId=6D94E307-67D9-41AC-B2D6-0074D6286FA9&displaylang=en
¼br> WSS 3.0 SDK 1.2:  Includes Conceptual and Class Library Reference documentation, Web Services documentation, and Developer Tools and Samples for WSS technology *only*. http://www.microsoft.com/downloads/details.aspx?familyid=05E0DD12-8394-402B-8936-A07FE8AFAFFD&displaylang=en  

As you can see in the download page there has been a ton of new features added to this release such as follow:

  1. New Start Menu shortcut : A quicker access to documentation and welcome page
  2. You now have a choice of installation location when you’re installing the SDK 
  3. Offline Experience Improvements: More Technical Articles, Visual How-to Articles, and Book Excerpts—plus the Excel Services and Excel 2007 Windows Compute Cluster Server 2003 Job Submission Developer Guide are packaged into one searchable CHM file ( Some of the elements such as WMV files and screencasts are still not available offline)
  4. New Tools and samples: There are many new tools and samples included in this release as follow:
    • Microsoft Business Data Catalog Definition Editor: I am so glad that what I was shouting here  8 months ago finally came into the consideration.Although it is not as complete as BDC Meta Man, I’m sure it is still better than nothing:)
    • WSHelloWorld Web Service
    • Excel Services User Defined Function Sample
    • WSOrders Custom Proxy Sample
    • SAP Sample
    • Sample Protocol Handler
    • Custom Content Source
    • IRM Document Protector
  5. Revised MOSS SDK conceptual topics such as :
    • How to: Create a Minimal Master Page
    • Provisioning Portal Sites
    • Portal Site Template File
    • Portal (Portal Site Template)
    • Webs (Portal Site Template)
    • Web (Portal Site Template)
  6. New MOSS SDK conceptual topics such as :
    • How to: Customize RSS for the Content Query Web Part
    • How to Create a Web Service Connection by using the Business Data Catalog Definition Editor
    • How to Create a Database Connection by using the Business Data Catalog Definition Editor

Here are the links to the online versions:

http://msdn2.microsoft.com/en-us/library/ms550992.aspx

http://msdn2.microsoft.com/en-us/library/ms441339.aspx

Categories: MOSS 2007 Tags:

Landing a site collection in its own content database

August 24th, 2007 No comments

One of the many advantages of having site collections over just sub sites is that you can create a content database for each site collection. One of the obvious advantages is when backing up or restoring different pieces of your farm that makes it easier to deal with multiple content databases rather than just a giant content database. There are many other advantages that I am not going to write about mainly because it’s been fully documented at TechNet site.

Unfortunately, there is no a straight way to tell MOSS where you want your site collections to land when you move them around or create new ones. MOSS uses a round-robin algorithm to distribute them evenly in the existing content databases. However, there is a UI trick (also used in SPS2003) which still can be used in MOSS 2007.A few days ago I was going through the same process for a client and I thought that it is not a bad idea to blog it here.

We were basically moving a site collection from DEV to QA, both environments were 32-bit, same topology and etc. In other words they were identical in many ways.

DEV :

DEV-1) Run stsadm to back up the site collection
stsadm -o backup -url http://mossdev/mysitecollecionDev  -filename c:\mysitecollecionDev.dat

DEV-2) Copy DAT file (mysitecollecionDev.dat) to QA

QA:

QA-1) Choose between QA-1-1 or QA-1-2

QA-1-1) ARE YOU MOVING IT TO AN EXISTING WEB APPLICATION?

       1) Go to content databases in central admin
       2) Choose your web application name from the drop down box
       3) Take all existing content databases offline (status=offline).
          This doesn’t actually take the Site Collections offline, but only prevents any new SiteCollections from being created in these database and that’s exactly what we want.
       4) Go to QA-2

QA-1-2) ARE YOU MOVING IT TO A NEW WEB APPLICATION?

      1) Create a new web application
      2) Go to QA-2

QA-2)Create mysitecollecionQA as an “Explicit Inclusion”  managed path (Central Administration–> Application Management–> Managed Paths)
QA-3)Create a site collection exactly in the same level as it was in DEV. In this example it should be http://mossdev/mysitecollecionQA. Make sure that the name you choose for content database at level is the name you want to keep for ever ,because we are going to overwrite into this content database
 ¼br> QA-4) Run stsadm to restore the datafile you moved earlier

stsadm -o restore -url  http://mossdev/mysitecollecionQA -filename c:\mysitecollecionDev.dat  -overwrite

QA-5) Confirm that your new site collection is created in the only online content DB that you just created and change the status for all the other databases back to online.

Categories: MOSS 2007 Tags: