Skip to main content

SharePoint 2010 - Items Added By Anonymous Users Cannot Start Workflows

On a public-facing website on SharePoint 2010, I was building a simple list to be used as the Contact Us form, so that when an item is added to the list, a workflow will run which sends an email to the owner of the website.

It all worked fine until I tested as an anonymous user that wasn't logged in.  I could create the item fine, but the workflow would not run.  The reason for this is, when an item is created by an anonymous user, the item does not set an author against itself (created by, modified by), which means the Workflow does not know what permissions it should use to run.

Turns out there's really only one way to get around this.  Open up your old mate Visual Studio and create an Event Receiver on the ItemAdded event.  What needs to happen is as soon as the item is added to the list, the event receiver will grab the item, and set the Author value to something generic (I used the System Account), then run the workflow.

(code in text at the bottom of this blog)


However, after deploying the solution to the site, I was still having issues with the Event Receiver & Workflow refusing to run and returning an error to the Event Viewer:

Error loading and running event receiver ExecuteWorkflow.EventReceiver1.EventReceiver1 in ExecuteWorkflow, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0e007c3794fb9704. Additional information is below.
: <nativehr>0x80070005</nativehr><nativestack></nativestack>

After much hair pulling, I now realised I should have been face slapping myself, as the issue was happening because I had set Advanced Settings on the list so that users could only see and edit items that were created by themselves.  This was causing the event receiver to say 'hang on buddy, an item has been added, but I can't read it'.

Changed those settings back to being able to view and edit all items and the event receiver worked fine.

To get around the issues with Anonymous users being able to view all the data in the list, you have two options:
- Get the workflow to delete the item after it's details have been emailed.
- If you need to report on the list data (it needs to be kept), Get the workflow to copy the item to an identical list that Anonymous cannot access.

--------------------------------------------------
using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace SetAuthorForWorkflow.EventReceiver1
{
    
    /// <summary>
    /// List Item Events
    /// </summary>
    public class EventReceiver1 : SPItemEventReceiver
    {
       /// <summary>
       /// An item was added.       
       /// </summary>
       ///       
       public override void ItemAdded(SPItemEventProperties properties)
       {
           if (properties.ListTitle == "BookingList"  || properties.ListTitle == "ContactUsList")
           {
                Guid siteID, webID, listID;
                int itemID;
                listID = properties.ListId;
                itemID = properties.ListItem.ID;
                using (SPWeb web = properties.OpenWeb())
                {
                    siteID = web.Site.ID;
                    webID = web.ID;
                }
                //run this block as System Account
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(siteID))
                    {
                        using (SPWeb web = site.OpenWeb(webID))
                        {
                            web.AllowUnsafeUpdates = true;
                            SPListItem item = web.Lists[listID].GetItemById(itemID);
                            if (item != null)
                            {
                                //Impersonate the Author to be System Account
                                item["Author"] = web.AllUsers[@"SHAREPOINT\system"];
                                item.SystemUpdate();
                                //start the by add the item and workflow name
                                if (properties.ListTitle == "BookingList") { StartWorkflow(item, "EmailBooking"); }
                                else if (properties.ListTitle == "ContactUsList") { StartWorkflow(item, "ContactUs"); }
                            }
                            web.AllowUnsafeUpdates = false;
                        }
                    }
                });
           }
           else
           {
               return;
           }
       }

       private static void StartWorkflow(SPListItem listItem, string workflowName)
       {
           SPWorkflowAssociation wfAssoc = listItem.ParentList.WorkflowAssociations.GetAssociationByName(workflowName, System.Globalization.CultureInfo.CurrentCulture);
           listItem.Web.Site.WorkflowManager.StartWorkflow(listItem, wfAssoc, wfAssoc.AssociationData, true);
           listItem.Update();
       }
    }
}
------------------------------------------

Comments

Post a Comment

Popular posts from this blog

Export Group Membership From Active Directory Using Power Query

If you need to export a list of staff from an Active Directory Group, follow these steps.  Allows you to gather all the names & usernames of staff in a particular group and save the information into Excel.

Step By Step How To:
Open up Excel > Power Query TabClick From Other Sources > From Active Directory:(In Excel 2016 this is found under: Data Tab > New Query > From Other Sources > From Active Directory) screenshots from Excel 2016 on left, Excel 2010 w/ Power Query Add-on right A popup will ask you to enter your domain name.  It should already be populated with the correct information but if not, input your companies domain name & Click OK:
In the Navigator that opens up, expand your domain tree and double click group:

You should now see a list of all groups in Active Directory in the Power Query Editor:
In order to find & filter for a specific Group, Click the Down-Arrow for the 'distinguishedName' column > Text Filters > Contains...
Type the name…

HP Laserjet 1022 Printer - Can't Print on Windows 10

If you are having trouble getting your laserjet working on Windows 10, here's how I resolved the problem:

Go to Devices & PrintersFind your printer & go to Printer Properties > Advanced TabSet Driver to: HP LaserJet 1022 Class Driver & ApplyIf HP LaserJet 1022 Class Driver is not available.  download from this site: http://support.hp.com/us-en/drivers/selfservice/HP-LaserJet-1000-Printer-series/439424/model/439431Print a test page.


SharePoint Online - Branding with CSS

-----------------------------------------------------------------------------------------
This post is related to a larger group of posts called Migrate SharePoint to Office 365 - Planning & Steps
-----------------------------------------------------------------------------------------

I've gone with a Publishing Site Collection so that I can use the 'Alternate CSS URL' in Site Settings > Master Page.

First off, thanks to the following websites which helped get me started:
http://blog.sharepointexperience.com/2015/02/sptechcon-austin-february-2015/#more-2766http://blog.sharepointexperience.com/2015/01/to-brand-or-not-to-brand/ Here's what the end result will look like (if you have the same fonts):


Now for the CSS...

/* Design By : Brett Randall*/
/*     Design Colours: Greens/Greys/Blues */
/*     Last Modified : 28/02/2017*/
/*     Description: CSS to rebrand SharePoint Online Publishing Site Collection that is using seattle.master */

/* TABLE OF CONTENTS
-- -…