Archive for February 28, 2011

Impersonate SharePoint User

This is a post that I’d imagine most people who have been developing with SharePoint will be aware of but I thought it might be worthwhile blogging about.

I’ve seen code where people have tried to impersonate a user to perform actions against a SharePoint file by using the LogonUser function.

public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, out int phToken);

When working with SharePoint, there is no need for this. For starters, the above function requires you to know the user’s password. Now, of course, there are good reasons why the business rules may require this and I’m not saying it shouldn’t be implemented that way. For the scenarios where this is not required, there’s a very easy and simple way of impersonating a valid SharePoint user, without having to know their password.
Read more

Temporarily Disable SharePoint Events

Just a quick post to demonstrate how you can disable events (item, list, web and workflow) for the currently running thread.

There are many reasons why you may want to do this, one of which could be for a unit test where you programmatically create a list item, execute tests against it and then as part of the clean up process, delete the list item. If you have an ItemDeleting event defined against the item that prohibits deletions, then you’ll end up with potentially lots of stray unit test files in your library or list. You can use SystemUpdate to perform changes against an item without events firing, but there’s no way out of the box to stop other events from firing, at least not without a little extra coding.

This solution is pretty simple and concise.

First, create a class that inherits from one of the event receiver classes (see SPEventReceiverBase Class.
Read more

External Participant for Task Items

If you’re wondering why you get (External Participant) appended to tasks created with the CreateTask and CreateTaskWithContentType workflow activities and are investigating why it appears that the person who was assigned the task is being marked as an external participant, then it may be worth making sure you didn’t do what I did….. 🙂

I was assigning the task to the person who created the document responsible for the workflow that created the task and as they were a local machine user with a profile I didn’t get why SharePoint was marking the task as an external participant.

Looking at the XML document for the task I noticed the following element:
Read more

Coding the Creation of Information Management Audit Policy

This is a quick post to demonstrate how you would create or update the Information Rights audit policy for a specific content type in SharePoint.

First things first, lets have a look at the method that implements this for us:

public void EnsureAuditPolicy(SPContentType ct)
    const string AUDIT_POLICY_NAME = "Custom Audit Policy";
    // Policy XML for enabling all out of the box auditing
    const string AUDIT_POLICY = "<audit><update></update><view></view><checkinout></checkinout><movecopy></movecopy><deleterestore></deleterestore></audit>";
    Policy policy = Policy.GetPolicy(ct);
    if (policy == null)
        // Create new custom policy
        Policy.CreatePolicy(ct, null);
        // Retrieve newly created policy
        policy = Policy.GetPolicy(ct);
        if (policy == null)
            throw new NullReferenceException("policy");
        // Update policy information
        policy.Statement = string.Format("Custom audit policy for viewing, editing, checking in or out, moving or copying and deleting or restoring \"{0}\" items", policy.Name);
        policy.Name = string.Format("{0} - {1}", policy.Name, AUDIT_POLICY_NAME);
        policy.Description = "Custom Audit Policy";
    if (policy != null)
        // The PolicyId property provides the policyFeatureId parameter for the PolicyItemCollection indexer
        PolicyItem auditItem = policy.Items[PolicyAudit.PolicyId];
        if (auditItem == null)
            // Update policy with custom audit requirements
            policy.Items.Add(PolicyAudit.PolicyId, AUDIT_POLICY);
            // Modify existing policy with the custom audit requirements
            auditItem.CustomData = AUDIT_POLICY;

Read more


Get every new post delivered to your Inbox

Join other followers: