Monday, March 24, 2008

Update SPListItem Created By and Modified By Fields

Well I thought I would have the simple task which was not so simple. I had a requirement to allow users to make anonymous submissions which I wanted to store in a SharePoint List. Simple enough right?

I needed to create a web part and in that web part I needed to create a SPListItem for designated list. In a previous blog I had created an archive method where I had set the modified by and last updated but apparently I had made a mistake. The code will absolutely work if I am logged in as an administrator however if I am logged in as user who is part of a member group with contributor rights I cannot set the created or modified by fields. This made sense for what I was doing at the time but now I needed to allow normal every day collaboration users to do this operation. As well, the users in the members group should not have rights to edit or delete items in the list items; just add. It is simple enough to break the security inheritance on the list and associate members of that group to a custom permission level. However I wanted to avoid doing that.

I did some searching around and found the following:

These show you how to update the created and modified fields but did not help me in my quest.

I found some stuff that a guy wrote on creating an impersonation utility:

However that was going really overboard. In WSS 3.0 you now have the ability to SPSecurity.RunWithElevatedPrivileges. Now I had tried this in my web part but it was not going so well. However I moved the logic to a custom ItemAdded event handler on the list and that did the trick. Plus it is better to put the logic of changing the created and modified by on the list itself because if direct submissions are made on the list then logic is centrally located.

The event handler is below:


public class InsertEventHandler : SPItemEventReceiver
{

public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);
ChangeSubmittedBy(properties);
}

private void ChangeSubmittedBy(SPItemEventProperties properties) {

Guid siteID = properties.ListItem.ParentList.ParentWeb.Site.ID;
Guid webID = properties.ListItem.ParentList.ParentWeb.ID;
Guid listID = properties.ListItem.ParentList.ID;
Guid newItemID = properties.ListItem.UniqueId;

SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(siteID))
{
using (SPWeb web = site.OpenWeb(webID))
{
SPList questionList = web.Lists[listID];
SPListItem listItem = questionList.Items[newItemID];

SPFieldUserValue oUser = new SPFieldUserValue(
web, web.CurrentUser.ID, web.CurrentUser.LoginName);

listItem["Author"] = oUser;
listItem["Editor"] = oUser;
listItem.Update();
}
}
});
}
}

Tuesday, March 18, 2008

Some BlackPearl Install Notes 1

I just finished another install of K2 BlackPearl and ran into a couple things I had not run into in the past that you should know about.

1 – Visual Studio 2005 Extensions for .NET Framework 3.0 (WCF & WPF)

Quick note if you are trying to install Visual Studio 2005 extensions for .NET Framework 3.0 (WCF & WPF) and you have .Net 3 SP1 installed you will run into some issues. Use the following to help for some more information - http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2912680&SiteID=1

You either need to un-install .Net 3.0, install the extensions and then upgrade to .Net 3.0 SP1.

If you do not want to do this the link above has a command line which should resolve the problem: msiexec /i vsextwfx.msi WRC_INSTALLED_OVERRIDE=1

2 – Installing Microsoft Report Viewer Redistributable 2005

This was weird. We installed Microsoft Report Viewer Redistributable 2005 and then got an error in the K2 Workspace when trying to access Reporting Services saying we need to install Microsoft Report Viewer Redistributable 2005 SP1. We down loaded Microsoft Report Viewer Redistributable 2005 SP1 (Full Installation) but that would never work.

The solution was to install Microsoft Report Viewer Redistributable 2005 then install Microsoft Report Viewer Redistributable 2005 SP1 (Upgrade). The upgrade is referenced in the Getting Starting document but the original distributable is not.

3 – DTC Configuration

The Getting Started document mentions this but do not forget to configure the DTC security configuration on SQL Server, the K2 Server and possibly on the MOSS server if MOSS and K2 on different machines. You will run into deployment issues if it is not configured on each machine.