I have already discussed about how to generate an automatic ID in Infopath with the help of Programming. Well, many people are not very much familiar from writing code and they prefer a no code solution. So, I thought of explaining the same concept without code.
There are certain inbuilt functions in Infopath which many times save your hell lot of effort and also provides good flexibility to manage your applications. One of them is the "Count" function which, as the name suggests, count the number of items in the node. We will make use of this function to generate an ID.
Let us take the same example which I took in the last post to explain the same concept but with code. We have an Infopath form which we use to file the review meetings of the project every week. The submitted Infopath form is named in such a way that the name is the concatenation of the name of the Project and the next count. Like, If the Project name is "A" and already 5 items for this project are there in the list then the name of the next infopath form will be : A_6. Project name is entered by the user while filling the form. The number "6" is calculated with the help of the "Count" function in the Infopath form and let us see how.
Design the Infopath form by creating all the fields like Project Name, Today's Date etc. as per your requirements. Publish this Infopath form and make it as a content type in the form library. Then we need to make a receive data connection referring this Library and click the field "Project_Name" while making this connection as it will ask you to select the fields whose values you want to enter in the form. Now create a field in the Infopath form and name it "ItemsCount". Open the properties of this field and click the button just next to the place where you enter the default value. Select the "Count()" function and you will see that it will be asking you to enter a field from the form as a parameter. Click on that parameter and select the receive data connection you just created. It will show you the field "Project_Name" as while creating this connection, you clicked it. Select this field and instead of clicking the "Ok" button after that, just click the "Filter" button. Click on "Add" button and you will see three drop down boxes. Now select the "ProjectName" field of the Infopath form in the first drop down box. In the second drop down box, select "is equal to" and in the third drop down, select the receive data connection and in that select "Project_Name"(This is the field in the Form Library) . In short, we have written a query that will give us the count of the items in the form library where the project name is the one which is entered by the user. So if the user selected Project "A" in the Project Name field in Infopath then this will give us the count of the number of items where the Project name is "A" in the list.
Now create a new field and name it "ID". Open its properties and from the list of inbuilt functions, select "Concat()". This function is used for concatenation and by using this we will concatenate two fields. The default value of the ID field will be :
concat(ProjectName, "_", (ItemsCount + 1))
Here, ProjectName and ItemsCount are the field from the Infopath form. As you can see that we have added 1 to the ItemsCount function, so we will always get the next ID while submitting this form. Make the "ID" field as name of the submitted form and it will always give you the next number.
(I am so sorry for not giving the screenshots)..........
In my last post, I explained this same concept with a CODE solution. Well, the choice completely depends upon the application's requirements, its complexity and your ease. I use both the solutions depending on several factors. I prefer a no code solution when my application is simple and I just need to generate an ID. But if my application is complex and I am aware that further more requirements will be introduced then I go for a Code solution as it provides great flexibility to change your application. That is why I wrote two different posts to explain the same concept so that users can decide their option depending upon several factors.
If possible, please spend little time to poll or comment that whether this post was helpful to you or not. Your feedback matter a lot as that will enable me to improve the posts for my readers.
Posted by Ginni Atul Sharma at 9:38 PM 1 comments
Friday, November 26, 2010
Auto Generating ID in Infopath (With Code)
Things would have been so easy if there would have been an autogenating ID feature in Infopath form library. To track the number of items, a column naming "ID" is there in every list but it does not serve any useful purpose when an item is deleted. This is because, it is not effected by the deletion of the items and keep counting them as and when the items are added. So if 5 items are added and you delete one of them, then the next count will be 6 only and not 5.
Well, things are not that much difficult and I figured out an easy solution to achieve this task. But with one condition and that is to use SharePoint Object Model. We will start this with an example as that will make things very clear.
Let us suppose that we are designing an Infopath form with programming and I am writing this code in the submit event handler. We have a scenario of calling meetings of a particular project by filling the Infopath form. Once the form is submitted, a mail goes to all the team members about the details of the meeting and the submit title of the form is "Project Name_Meetings Count". This means that if Project A is there and if I am filling its 5th meeting then the title will be "Project A_5". This value is generated automatically through code in the submit event handler and is then assigned to a field in the Infopath form. The most important thing over here is the count and lets see that how is this calculated.
We have a field in the Infopath form naming "Project Name" which the user enters while filling the form. When he submits the form, I run a CAML Query to calculate the count of the items, having the name of the Project specified by the person in the form. Then this count is added by one to calculate the next ID and this ID is then assigned to the Infopath field "SubmitName" which is the name of the submitted form.
//First Retrieving the value entered by the User
XPathNavigator MyNavigator = MainDataSource.CreateNavigator();
string ProjName = MyNavigator.SelectSingleNode("//my:ProjectName", NameSpaceManager).Value;
SPWeb MyWeb = null;
using SPSecurity.RunwithElevatedPrivilidges(delegate ()
using(SPSite MySite = new MySite("Your site URL"))
MyWeb = MySite.OpenWeb();
//Retrieving the List
SPList MyList = MyWeb.GetList("Your List URL");
string FieldIntName = MyList.Fields["Project Name"].InternalName.ToString();
//Running CAML Query to get the count of the Project Name entered by the User.
SPQuery MyQuery = new SPQuery();
MyQuery.Query = (See this Link to write a CAML query) http://sharepointmagazine.net/technical/development/writing-caml-queries-for-retrieving-list-items-from-a-sharepoint-list
In the Field Ref, you need to write "FieldIntName", which is the Internal Name of the Project name and this is a column in the list.
In the Value, you need to write "ProjName", which is the name of the Project entered by the user while filling the form.
//Fetching all the items satisfying the above query.
SPListItemCollection AllItems = MyList.GetItems(MyQuery);
int Count = AllItems.Count; //This gives us the count of the items having the project name entered by the user.
In order to create a new ID, we will add one to the total count and will concatenate this ID with the Project Name. So like this, our ID will automatically be generated every time a new item is added. And, it will not be affected with the deletion of items as the count is calculated on the basis of items added in the list.
//Final ID will be :
string SubmitID = ProjName + "_" + (Count + 1);
//Assign this to the field in the infopath form which will be the name of the submitted form
XPathNavigator formNav = MainDataSource.CreateNavigator;
Now when the form is submitted, a CAML query is run to calculate the count. And then, one is added to the total count to generate the next ID.
The same thing can be done without programming also which I will be explaining in my next post. I know that you all must be wondering that if this is possible without programming then why to go for a programming option. Well, the answer is that the programming option gives you great flexibility and you can further customize your application depending upon the requirements. I use both the options depending upon the complexity of my task. If i just need to generate an ID then I go with no code solution. But if I have to do several other complex tasks then I go with programming option. Always, the choice is your.