SharePoint: Unique column values - sharepoint

I Want to have only unique values in a SharePoin List. To achieve this I can use 'ItemAdding' event handler as mentioned in the below link.
http://weblogs.asp.net/vikram/archive/2008/12/24/sharepoint-using-event-handler-to-make-a-column-unique.aspx
Now I have a Doubt: Suppose that two user tries to add list Item in the list with the same column value(which requires unique value) at the same Time. will ItemAdding event would be fired at the same time for both call? If so then there is a possibility that two items having same value in the column. Please confirm.

I think, this method allows you only to have semi-unique values in the column. That is, if everything is normal, it will be unique. However, you cannot compare the properties of two items being added simultaneously (and yes, it may happen, since IIS web server is multi-threaded). Hence you might have duplicate values as a result.
A workaround - implement a "ItemAdded" event handler, too and delete a newly added item, if it has that property the same as some other existing item. This will, of course, happen very rarely.

If you have enough concurrent users that this is likely to be a problem, SharePoint probably isn't the right tool for the job. If the value is created by the user, it is extremely unlikely that two users will be adding the same value at the same time. If it is automatically generated, you probably should be using something based on ID, which is known to be unique.
If you are worried about the possibility of duplicates anyway, just make sure that the field can be edited if you do end up with duplicates causing problems.

Related

SharePoint send notification by assignation

I created in SharePoint a list. It has several columns, one of these columns is called assined to. It contains one or more person.
What i want, is that an email is send to the new person/s, if a new person is added to this column.
Now i searched a little bit, and found out that I should use Workflows to solve this problem. But the problem is the Workflow is started only automaticly if any changes are made in an element or when an element is created.
So my question is, is theire a possibility to trigger an email by content changes in a specified column.
By the way im working with a list in SharePoint 2013.
One way to accomplish this is to use an additional column on the list to track the previous assignment. Whenever an item is modified, the workflow can run and check the Assigned To field against the new Previous Assignment field. If they are different, the workflow can send the email and then update the Previous Assignment field to be equal to the current Assigned To value.
You can make the Previous Assignment field hidden from forms in the content type settings so that it won't be visible (and confusing) to users.
Yes, this would work. You would need to create a condition which checks on every change which is made to the item if the field "assigned to" is filled.
Here is nearly the same requirement at the MSDN Forums:
https://social.msdn.microsoft.com/Forums/office/en-US/112c2be9-5d3e-47d1-ad1c-06312de8a925/workflow-condition-assigned-to-is-not-empty?forum=sharepointcustomization

Calculated SharePoint Column

I have a simple calculated column in my SharePoint 2010 list. It takes the list item ID, adds 100 to it.
When my users are creating items in the list, the calculated column does not get updated unless I go in, edit the column (do nothing) and save it. It, in fact, gives all items a value of 101 unless I manually edit the column.
Is this typical or is there a work around for this issue?
Thank you!
It is not possible to create calculated column based on ID value. The Id of the item is created after the item is added to the list.
You should use workflow instead.
The problem with using a workflow to do this (as per the accepted answer) is that workflow can take an appreciable time to execute. So you cannot create the ID until AFTER the new item is saved and there is always the danger that simultaneous users can create ID clashes that you also have to handle. If the workflow (as on a busy system) takes several minutes to work, you can also get the problem of someone else editing the item before the workflow has finished which may cause the workflow to fail leaving the item without any ID.
As an alternative, you might consider using JavaScript in the NewItem.aspx page to lookup and increment a counter from a separate list. Note that you have to update the counter as you read it if you are doing this so as to ensure that other users don't accidentally get the same ID if creating entries at the same time. This means that you must not mind the counter incrementing even if a user subsequently cancels the new item without saving.
As you noticed in opening/saving an item, The Calculated Column is updated on every item change.
Does it work to have a Workflow read the Title and write (the same) Title?
The [ID] reference in the Calculated Column should be set.
No need for an extra LookupID column then.

Sharepoint: Integrity of lookup fields after a list import

I got a question about the behavior of lookup fields when importing data. I wonder how the lookup fields behave when the list they point to is being replaced/imported. To explain the issue, I will provide a quick example below:
As example, assume we have these two sharepoint lists:
Product Types
-------------
+ Type Name
+ Code Nr
+ etc
Products
--------
+ Product Name
+ Product Type (Lookup field to list "Product Types")
+ etc
In my scenario, the Products List contains production data on the production Sharepoint platform. It is filled with data by the business users.
However the Product Types list contains rather static data and is maintained by the developer.
Now after a development cycle, the developer wants to deploy his new webparts and his new data (product types list). The developer performs the following procedure:
On the dev machine: Export "product type" list using stsadm
On the production machine: Delete all items in the "product type" list
On the production machine: Import the "product type" list using stsadm
This means we basically replace the "product type" list on the production server while keeping the "product" list as it is.
Now the question:
Is this safe? Will the lookup references break under certain circumstances?
Any downside of this import/export procedure?
What happens if someone accesses a "product" during the import? Will the (now invalid) reference clear its own content (become a null value).
What happens if the schema of the "product type" list changes (new column)? Will this cause any troubles?
Thanks for all feedback and suggestions!
Update 1
The imported "product type" items have the same IDs as previously deleted ones.
Update 2
Started a bounty to get some more feedback/opinions.
We have had this exact same scenario before. This is a little tricky, depending upon how you will approach it.
1) Delete and Recreate Product Type list through UI
If you delete and recreate the lookup List(Product Type in your case) through UI, then you will lose the connections because the List's id GUID will change upon recreation. So do not go that route.
2) Delete and Recreate Product Type through a Feature
If you had created the Product Type list through a feature.xml file using the <ListInstance> element, then if you delete that list and then recreate it using the same feature (basically Id attribute of ListInstance remains the same, number of list items, i.e. the number of <Row> elements, may change), the association would be maintained. So if you were adding 5 more product types, then if you had created the list using a feature, you could just delete the list and provision the new one using the same feature with extra info for new items and everything would just work!
As a side note, this is the better approach because if you have to do the upgrade on a lot of servers, then rather than doing list export import via stsadm, feature deactivation and activation is a much more recommended solution. This is how we did it.
3)Deleting all list items from Product Type and adding new ones (list is never deleted)
If you are linking the lookup field (in Product List) to the ID field of the lookup list(Product Type), you have to remember that ID is auto-incrementing, so if you delete all items and then add new ones, then their ID's would be different. Say you had 5 items with ID's (ID field is not shown in UI while editing in Datasheet view) 1-5 in the list. If you delete them and add new items, their ID's would start from 6 and not 1 again. So if your lookup field had link to the item with ID 1 in it, then this method is not going to work because there is no item with ID 1 in the Product Type list anymore. So you might want to really try this out before going to production with this method.
4) Editing the list in place
If the list is not extraordinarily huge, and you only have to make this change to one or two instances, could you not just edit the list directly in the datasheet view on the prod server? When editing in datasheet view, do not delete the item, but just overwrite the values of its columns. And you can add more items if you want. This will make sure your ID's are valid.
I have mostly talked about adding new items to the list. Now if you were deleting existing items, then your lookup fields will be affected because assuming you linked the field by ID, the ID is not present anymore since the item has been deleted. Basically, any method you use, maintaining your ID's is critical.
Now regarding your doubts/questions:
I am not too sure about stsadm export import for a list (never done it myself), but stsadm can be tricky as some operations will work on certain scopes only. So you better try out your exact scenario on a dev env.
What happens during an import is tricky again depending on the exact timing. I am sure SP has its own concurrency mechanisms, but you cannot have a definitive answer as it might probably be different based on the stage of the import. If possible, recommended approach is to do the import during a planned downtime.
Regarding changing schema of the list, a change in the schema of a list will not affect the existing list instances (for the most part). If you do this through UI, I believe SP makes changes to the content DB directly. I am not certain how you intend to do this, but if you were to add a column to an existing list using a feature, the way to do this is during feature activation by adding a new content type to the list and adding your new column to this content type. This way you add the column but do not affect the existing list items.
Good luck...
There are two components to a particular lookup: the field, and the field value. The field value only contains the ID of the item(s) it refers to, and the display field. This information is meaningless without the field, which specifies what list to look at and what field to use as the display field.
The primary reason that a Lookup will break occurs on the field scope: either the list it referred to no longer exists, or the list does not contain the required field. These would generally happen if you deleted and recreated the list, but you aren't doing that. If you do break a lookup's list reference, then the only thing you can do is re-create the lookup, because you cannot configure the list reference for a lookup field once it is created.
The downside of your import/export procedure is that you lose the validity of all currently existing lookup values. A lookup maintains its integrity based on the ID of the item it references. So when the display field changes, it still refers to the same item. If you delete the item, then the lookup no longer references it, even if you create a new item that has the same value for the display field. So you would have to reassign all of the products to the new product types.
It should be noted that if you were to revert the deletion of that item, it would return to being on the lookup! The reference to that ID is kept until the actual lookup value is updated (such as by editing the Product).
All of your now invalid references will be null for purposes of interaction. You won't see anything on display forms, and you won't have the options when you try to update the product. When you do update the product, you update it to what you just set it to, which since you can't set the non-existent IDs, means that there are no more references to those IDs.
Any changes to the Product Type list's schema that do not affect the display field specified for the lookup will not have any effect on the lookup integrity. If you do change the display field in any fashion, and of course if you delete it, then it will break in the same fashion as with the list reference. However, you can set the display field, both in the UI and in the object model, so it is easy to fix this.

SharePoint List Pre-Population

I have two lists, each on different tabs (sub-sites) of the site. I would like users enter data on the first tab and somehow copy a portion of that data over to the second list automatically (like a pre-population of several of the fields on the second list). What would be involved in making this happen? Of the ideas I have come up with, none seem too pragmatic.
An item receiver on the first list would allow you to insert data into another list at the time of creation.
Depending on your situation it may be worth trying list look ups (one to many).

Auto number column in SharePoint list

In a SharePoint list I want an auto number column that as I add to the list gets incremented. How best can I go about this?
Sharepoint Lists automatically have an column with "ID" which auto increments. You simply need to select this column from the "modify view" screen to view it.
You can't add a new unique auto-generated ID to a SharePoint list, but there already is one there! If you edit the "All Items" view you will see a list of columns that do not have the display option checked.
There are quite a few of these columns that exist but that are never displayed, like "Created By" and "Created". These fields are used within SharePoint, but they are not displayed by default so as not to clutter up the display. You can't edit these fields, but you can display them to the user. if you check the "Display" box beside the ID field you will get a unique and auto-generated ID field displayed in your list.
Check out: Unique ID in SharePoint list
If you want to control the formatting of the unique identifier you can create your own <FieldType> in SharePoint. MSDN also has a visual How-To. This basically means that you're creating a custom column.
WSS defines the Counter field type (which is what the ID column above is using). I've never had the need to re-use this or extend it, but it should be possible.
A solution might exist without creating a custom <FieldType>. For example: if you wanted unique IDs like CUST1, CUST2, ... it might be possible to create a Calculated column and use the value of the ID column in you formula (="CUST" & [ID]). I haven't tried this, but this should work :)
I had this issue with a custom list and while it's not possible to use the auto-generated ID column to create a calculated column, it is possible to use a workflow to do the heavy lifting.
I created a new workflow variable of type Number and set it to be the value of the ID column in the current item. Then it's simply a matter of calculating the custom column value and setting it - in my case I just needed the numbering to begin at 100,000.
it's in there by default. It's the id field.
If you want something beyond the ID column that's there in all lists, you're probably going to have to resort to an Event Receiver on the list that "calculates" what the value of your unique identified should be or using a custom field type that has the required logic embedded in this. Unfortunately, both of these options will require writing and deploying custom code to the server and deploying assemblies to the GAC, which can be frowned upon in environments where you don't have complete control over the servers.
If you don't need the unique identifier to show up immediately, you could probably generate it via a workflow (either with SharePoint Designer or a custom WF workflow built in Visual Studio).
Unfortunately, calculated columns, which seem like an obvious solution, won't work for this purpose because the ID is not yet assigned when the calculation is attempted. If you go in after the fact and edit the item, the calculation may achieve what you want, but on initial creation of a new item it will not be calculated correctly.
As stated, all objects in sharepoint contain some sort of unique identifier (often an integer based counter for list items, and GUIDs for lists).
That said, there is also a feature available at http://www.codeplex.com/features called "Unique Column Policy", designed to add an other column with a unique value. A complete writeup is available at http://scothillier.spaces.live.com/blog/cns!8F5DEA8AEA9E6FBB!293.entry
So I am not sure I can really think of why you would actually need a "site collection unique" id, so maybe you can comment and let us know what is actually trying to be accomplished here...
Either way, all items have a UniqueID property that is a GUID if you really need it: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splistitem.uniqueid.aspx
Peetha has the best idea, I've done the same with a custom list in our SP site. Using a workflow to auto increment is the best way, and it is not that difficult. Check this website out: http://splittingshares.wordpress.com/2008/04/11/auto-increment-a-number-in-a-new-list-item/
I give much appreciation to the person who posted that solution, it is very cool!!

Resources