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.
Related
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
I have a list (assume an issues list) and there is a workflow associated with it. The workflow can adjust the status column of the item to "Closed". Once an item's status is closed, I want to make it read-only so that noone can edit the item or create another workflow instance for that item.
What's the best way to achieve this?
There are item-level permissions that can be set, so you can override list-level permissions on an item-by-item basis. Adding this functionality into your existing workflow probably makes the most sense, but of course there is nothing out-of-the-box that SharePoint provides you.
Luckily, you can extend SharePoint's workflow by creating your own custom actions. The process for doing this in SP2010 is fundamentally the same as 2007; check out this MSDN tutorial for an overview of the process.
There is also a convenient package of custom activities provided in an open-source product called SPDActivities at CodePlex. Specifically of interest to you is the Grant Permission on Item activity. Even if you opt not to use the whole package, you can examine the source code and see about implementing your own version of it (I did something similar for a past project).
Once you have a workflow action for setting an item's permission level, simply add a step to your existing workflow to set Read permission for the affected audience or group.
Have you looked at SPUtility.js? You could get the value of your status field, and then if it is Closed, make the other fields read only (or hide them). This is done using JavaScript that is added in a content editor web part on your EditForm.aspx.
var myChoiceField = SPUtility.GetSPField('Single Choice Field');
if (myChoiceField.GetValue() == 'Closed') {
SPUtility.GetSPField('Field A').MakeReadOnly();
SPUtility.GetSPField('Field B').MakeReadOnly();
SPUtility.GetSPField('Field C').MakeReadOnly();
// etc..
}
Full disclosure.. this is an open source library I maintain. I've tested it with SharePoint 2007 only, but it may also work with SharePoint 2010 (unfortunately I don't have access to a SharePoint 2010 environment to test).
I would attack it one of two ways:
Once the workflow completes and sets the item to Closed, you can break the permissions inheritance from the parent list and set the list item permissions to read only. You could do this within a custom workflow or as either a Workflow or List Item event receiver.
Have an ItemUpdating List Item event receiver that sets properties.Status = SPEventReceiverStatus.CancelWithError if status is Closed.
Personally, I like the first option better as it fits in with SharePoint's security philosophy of not letting the user attempt what they do not have permissions to do. The following code is an example of setting read only permissions on a list item:
item.BreakRoleInheritance(false);
SPRoleDefinition role = web.RoleDefinitions.GetByType(SPRoleType.Reader);
SPRoleAssignment assignment = new SPRoleAssignment(web.AssociatedVisitorGroup);
assignment.RoleDefinitionBindings.Add(role);
item.RoleAssignments.Add(assignment);
Try creating an Event Receiver and handle the deleting event.
Idea (un-tested): Add a custom content type to the list (this will allow you control to edit as an admin later). Have workflow switch to the custom CT one the item is "closed". Add a read-only view of the item data to your EditForm.aspx, and in Designer add class "hidden" to your read-only view. Then add custom css in a CDWP on the page for class .hidden display:none. Then use JavaScript to add/remove that class based on the CT, so that the read-only view is only visible for the custom CT, while the edit wp is visible for all others.
Or use a custom InfoPath form to switch to read-only view based on status...
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
How to automatically print a form when a new item is posted to a list in SharePoint?
I created a list in SP, with customized Entry and Edit forms and a workflow that connects them. Every time a new item is posted the Entry form needs to be used, but when I click submit I need the workflow not only to post the item to the list, but also generate a 'Print form' that can either saved or printed.
Considering the event, adding an item triggers an SPItemEventReceiver.ItemAdded. You would call your printing code from within this event, and deploy the functionality through a Feature.
To give a really specific answer, you'll need to explain a bit more - where should the printing occur? On the machine from which the list was modified, or some central printing location?
More generally, Sharepoint can trigger a variety of events. Ishai Sagi's blog has a user-triggered solution that may give you some ideas. You could use a Javascript event as explained here. You could even send a specially-formatted email to a computer with Outlook running, with a rule that triggers a print.
Which is the best way to display a custom form for each new document being added to a specific document library?
I want the user to have some control over some actions that adding the document will cause - specifically, tasks created for users in a Task List, which the contributing user will have to OK before committing.
Is the best way to do this via a Workflow or an event handler?
Regards
Moo
For SharePoint server 2007:
A combination of a specialised content type and an event handler will give you the most control of the process (easier to debug too). A workflow is best for "easy" actions and is harder to make complex actions happen.
A workflow allows for more flexibility in assigning the workflow to different libraries by site admins.
Roll your content type and event handler into a feature that can be deployed. Assigning the content type to a list will allow you to take any data created by the standard form (remember you can create custom fields for really complex/custom data entry) and take any action required, including creating tasks based on the item.
Thanks for the answer, thats the route I was investigating but unfortunately I haven't found a way to make it work for Document Libraries - only Lists.
The problem I have come across is that aving a SPUtility.Redirect in the ItemAdded, ItemAdding, ItemCheckingIn or ItemCheckedIn event on the document library doesn't do anything at all, because there is no page related to the events - its all backend code being fired that is not linked to a web context, because its not the webpage doing the heavy lifting at that point but the Office integration.
All of the examples I have come across seem to rely on the fact that the EditForm.aspx or NewForm.aspx page is being displayed at the time the event is fired, which of course is not the case here.
This also leads to the problem that I cannot extend the EditForm.aspx or the CheckIn.aspx page to do what I want either, because Office 2007 circumvents both of these.
So, the only option left open to me at the moment is doing it through a workflow :/
Any further tips would be fantastic.
I may not understand your question, but is this close to what you're trying to do?
http://msdn.microsoft.com/en-us/library/ms550037.aspx
If not, in what way does this come up short?
A new "Content Type" with its New Form url set to the appropriate value should do the trick.
Unfortunately you may not be able to achieve what you are trying to with an Event Handler or a Workflow. You would ideally want the form to show up as soon as the user adds a new item to the library and a custom content type will do the trick.
Create a new Content Type. (Derive it from the existing Content Type)
Remove the default content type from the doc library.
Using the Object Model to set the New Form url to an aspx page or an Infopath form you've created.
Kind regards,