2

I am working on a Leave Application System using Sharepoint. I have added the total leave each user have in their user profile.

The user will apply leave and once it is approved, the leave taken will be deducted from their user profile. The workflow is done using the Approval Template.

So the question now is how do I proceed from here? How can I have the last step in the approval process to update the user profile total leave? Do I have to create a web part? If so, how I can pass parameter into web part from the approval workflow so that I can do the deduction?

Thanks

3 Answers 3

1

I would solve this in 2 steps:

  1. Make the Approval Workflow call some server side code;
  2. Let the server side code update the user's profile.

Make the Workflow call your code

  1. Create a event handler project in your Visual Studio for SharePoint
  2. In the WorkflowCompleted function put these code:

public override void WorkflowCompleted(SPWorkflowEventProperties properties)
{
    base.WorkflowCompleted(properties);
    if (properties.CompletionType != SPWorkflowEventCompletionType.Completed) return; //only update the Leave days when the leave request get approved.
    SPSecurity.RunWithElevatedPrivileges(delegate
    {
        using (SPSite site = new SPSite(properties.WebUrl))
        {
            using (SPWeb web = site.OpenWeb())
            {
                SPWorkflow wf = new SPWorkflow(web, properties.InstanceId);
                UpdateUserProfile(wf.AuthorUser.LoginName); //update the initiator's profile
            }
        }
    }
}

Update the user's prfile

public void UpdateUserProfile(string userAccount) 
{ 
    string url = "http://sp.adventure.com"; //replace with your site collection
    using(SPSite site = new SPSite(url)){
        SPServiceContext sc = SPServiceContext.GetContext(site);
        UserProfileManager upm = new UserProfileManager(sc); //now you have the manager object
        UserProfile up = upm.GetUserProfile(userAccount); //for example, "adventuer\\jony"
        up["Leave"] = int.Parse(up["Leave"].ToString())-1; //you may want to check if the Leave is more than 0
        up.Commit();
    }
}
0

How are you applying the leave count to a user profile? Do you have a custom list somewhere?

If that's the case, then a workflow should be triggered when the administrator accepts the request - Example: - User Leave (Custom list) - User Requests (Custom list with approval)

The workflow would do a list look-up based on the user that created the request: User Leave.CreatedBy = User Requests.CreatedBy

That should return only 1 record, which will have the User Leave count. Simply decrease that variable by 1

For more info, check out: Sharepoint - Create a workflow

0

One of the sessions I've been doing this year has been one that provides an intro to creating custom workflow activities that can be used in SharePoint designer. In there I have an example project that can read from the User Profiles. I'm using an example you provide a user name and a property you want to read and it returns the properties value. This could be used to get the current days available and a second, similar action could be created to write out a value back to the user profiles. As the slide deck explains, I feel very strongly about trying to leverage farm level services versus using lots of local sources like custom lists.

This page on my site has links to the slide deck and some source code to get you started. http://www.mikeoryszak.com/sharepoint/sps-the-conference-slides-and-additional-info

[Note, the source code is incomplete and since been significantly updated but not republished]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.