How to catch item changes on list? - sharepoint

I have created state workflow in Sharepoint.
My idea is:
1/ when workflow is started create copy of list item on which workflow was started, this copy is created to another web list within current site collection - that's working
2/ monitor changes on parent item and propagate them to copied item - that's working
3/ monitor changes on copied item and propagate them back to original item - that's not working
I was trying to use IListItemService, but apparently it canno watch for changes on different then current web ? Is there any other way how to do it ?
I was thinking about SharePoint 2010 Pluggable Workflow Services - but in my class which derives from SPWorkflowExternalDataExchangeService method CallEventHandler was called twice even when only one call of SPWorkflowExternalDataExchangeService.RaiseEvent was made (this was called from custom event receiver) - another problem with this solution is when I recycle app pool I lost my singleton class which is responsible for maintaining list of state information needed for RaiseEvent method.

There is an event mechanism built into SharePoint just for this purpose. There's no need to use workflows or copy items.
Example: Creating a List Item Event Handler

Related

How to create items in child list and trigger a child workflow from a Parent workflow

I want to know if we can have a parent workflow which when called leads to spawning of multiple child workflows. Is there a way we can actually implement it in SharePoint. So basically I want to use a part of my parent workflow to trigger another workflow - this particular workflow can be associated to the same list and sometimes they can be associated with different lists and libraries...will that cause any problems.
It would be awesome if you guys can share your thoughts and ideas regarding this topic.
So how I expect it to work :-
Let's say when something triggers workflow A it goes and spawns multiple items in the SharePoint list which triggers workflow B. And all the newly created items are running workflow B. Also if there is a way to call another workflow C which is linked to another library.
If you are using SharePoint 2013 or later:
Add a column to the list with a name like WFBtrigger or WFBstatus. Set it to default to "" or "Not run" or similar.
Create your Workflow A. When it's finished have it update WFBstatus to "Pending" or similar.
Create your Workflow B. Set it to start when the item is created. Add a Loop. In the loop wait for WFBstatus to change to "Pending". Do "B's" work. Update the WFBstatus field to "Done".
Now when A is triggered, B will shortly there after run.
To trigger C in another list, just have A or B update a "WFCstatus" column in the C list and have a workflow C waiting for that column to change.
So I have got some great news ! I was able to figure this one out but It is not exactly very direct
So SharePoint Designer allows you to create an item in another list using the create function. I have attached a screenshot
click here to view
So using this functionality you can create an item in a completely different list or the same list. But this has one problem- Once you actually create an item on the child list using a 2013 workflow it will not trigger the workflow in the child list. So this has been explicitly disabled to prevent a condition called Workflow recursion. So is there a way around it ?
Yes ! you have two options:-
1.Call the Rest Endpoint for the child workflow, Sample approach : https://blogs.msdn.microsoft.com/sridhara/2014/08/21/fix-sharepoint-2013-workflow-recursion-prevention-part-2/
2.Use MS Flow to create the flow in the child list
Hopefully it helps someone out there !
PS: For some weird reason for our company we cannot create 2010 workflows- apparently 2010 workflows are not supported in modern sites (I maybe misinformed)

Sharepoint 2013 App/Add-ins - one workflow multiple lists

I have 10 list instances based on the same template (CT) deployed in a Sharepoint 2013 Addins.
I would like to associate a workflow to theses list instances.
The trigger should be item updated.
Moreover i would like to pass a different value in a workflow arguments (ex text value)
Is it possible to run any type of scripts when an app / add ins is installed
(powershell or javascript)
Second option, is it possible to trigger a workflow with javascript when a item is updated
Thanks
If you write an event receiver in C# in Visual Studio 2013, it will catch each ItemUpdated event no matter for which list. WARNING: It also catches events from updates in system lists - hence you should either deactivate the event workflow whilst updating system issues or much better check your list names in the workflow in your event and levae all other lists run into event default behaviour.

create an eventreceiver for logging changes made by users in lists

I'm working on a SharePoint publishing site and I want to "log" all the changes made by users in a SharePoint list.
Some searches on the web guided me to the EventReceiver.
But my EventReceiver must be generic and attached to all lists in the site collection. The "log list" has several columns like the name of the list in which the event occurred, the name of the modified item, its old value and its new value.
Can someone guides me on how to achieve this?
There's already a provided answer for you on StackOverflow, hurrayy!!
It sounds possible. Create a class that inherits from SPItemEventReceiver and override ItemUpdating. You can use the following code to get the list:
using (SPWeb web = properties.OpenWeb())
{
SPList list = web.Lists[properties.ListId];
}
You can then use list to grab the list's title and URL. Next, compare each DictionaryEntry entry in properties.AfterProperties to the corresponding value in properties.ListItem to get your differences. Then save it to your log list. The trick would be automatically attaching your Event Receiver to each newly created list. Perhaps a timer job would work.
That said...
Before trying any of that, go to your site collection's Site Settings. Under Site Collection Administration, click Site collection audit settings. Under Specify the Document and Item events to audit, check Editing items. Only go with a custom solution if that does not give you what you need.
Would Version history not achieve the same functionality?
You can see what fields were changed by who and when. Though you would have to look at a list by list basis.
You can also access the version history information via the object model if you want to generate reports web part.
Otherwise using Janis Veinbergs link on how to attach a event handler to all lists and Rich Bennema method of getting the values from the updated item in a generic way, though I would use ItemUpdated as you don’t want to change the data that was updated only copy it to another location so there is no need to catch the data before it is submitted to the SharePoint database

SharePoint Workflow with replicatorActivity and onWorkflowItemChanged

Has anyone had success with adding an onWorkflowItemChanged Activity inside a replicatorActivity? It seems to only fire when outside the Replicator.
Scenario is the following:
Several users are assigned a task inside the replicator. If the Workflow Item is modified then we want to cancel all the tasks and assign new ones based on the change that was made. Problem is that onWorkflowItemChanged doesn't get fired when placed inside the replicatorActivity.
Yess, there is a solution (a workaround?)!

How to fire code upon creation of a SharePoint list?

I'm aware of the event receivers on a list for items added etc. However, I have not found a way to fire code upon the creation of a list.
What I'm trying to do is associate a workflow with a list when the list is created (by the user through the UI).
Anyone any ideas?
thanks.
There are a couple of routes you can take...
You can write your own list definition where you have defined the workflow association - That way all lists created based on your list definition, will contain your workflow on default.
Or... depending on your workflow... write an EventReceiver your attach to all lists of the type you wish to attach your workflow to (can easily be achieved tru a feature) and have your event receiver associate the workflow when the first item is added.
or you can associate the workflow to the contenttype used in the list (your own contenttype you attach to your own list definition or a default SharePoint contenttype)
I don't know the rest of your solution, so it's defficult for me to suggest the best solution for you.
What I (almost) always do, is write my own list definition - That way I can avoid problems like this, now or in the future.
With SharePoint 2010 it is now possible to hook into the list creation event by overriding the ListAdded event in the SPListEventReceiver class.
I usually deploy an extra view page which is set to the default view. When the user creates the list he will be sent to the viewpage which contains the setup in code behind. The view page then calls a method ive created, which changes the default view, removes the setup view and change any navigation node pointing to the setup view.
There is probably no perfect answer to this question because there is no list added event receiver (if memory serves me correct).
I don't know if this is the case, but if you really just needed to register an itemadded (or updated, deleted, etc.) event to any new list, I believe you can register the those events at the site (SPWeb) level and they will fire on any new lists created.

Resources