1

I have a problem of sequence in the list SharePoint 2010. I need a column Sequence with format 000001. This column is in type of single text.

I think the best way is customizing a workflow, but I cannot use SharePoint Designer, neither deploy a workflow as a WSP file.

I tried the column calculated who is equal to ID, but I must refresh the page to display the right number and the last number always 000000.

I want to try JavaScript to set the value of column. But the code does not work in my site:

$("input[title='title_of_the_field']").val('0'); 

I find the JavaScript for updating a list item in the site Microsoft. But it does not work.

http://msdn.microsoft.com/en-US/en-en/library/office/hh185011(v=office.14).aspx#sectionSection2

1 Answer 1

0

I was able to achieve this using JavaScript. Steps

  1. Open the NewForm page of the list. Example http://webappurl/sitecollection/site/lists/listname/newform.aspx
  2. Click Site Actions and Edit Page
  3. Add new web part -> Content Editor Web Part
  4. Edit HTML and insert below code

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    
    <script>
        function pad(num, size) {
            var s = num+"";
            while (s.length < size) s = "0" + s;
            return s;
        }
    
        //CHANGE SITECOLLECTION URL
        var siteUrl = '/sitecollection/site';
        function retrieveListItems() {
            var clientContext = new SP.ClientContext(siteUrl);
            //CHANGE test TO THE ACTUAL LIST NAME
            var oList = clientContext.get_web().get_lists().getByTitle('test');
    
            var camlQuery = new SP.CamlQuery();
            camlQuery.set_viewXml('<View><Query><OrderBy><FieldRef Name="ID" Ascending="FALSE" /></OrderBy></Query><RowLimit>1</RowLimit></View>');
            this.collListItem = oList.getItems(camlQuery);
    
            clientContext.load(collListItem);
    
            clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed))
        }
    
        function onQuerySucceeded(sender, args) {
            if(collListItem.get_count() == 0) {
                $('input[title^="Sequence"]').val('000001');
                return;
            }
            var listItemEnumerator = collListItem.getEnumerator();
            while (listItemEnumerator.moveNext()) {
                var oListItem = listItemEnumerator.get_current();
                var maxId = oListItem.get_id();
                maxId = maxId + 1
    
                var nextSequence = pad(maxId, 6);
    
                $('input[title^="Sequence"]').val(nextSequence);
            }    
        }
    
        function onQueryFailed(sender, args) {
    
            alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
        }
    
        ExecuteOrDelayUntilScriptLoaded(retrieveListItems, "sp.js");
    </script>
    
11
  • Hi Amal, thank you very much. I tried your script, but it does not work, there is no value in the column Sequence. And alert 'Request failed' does not appear.
    – AnnaCICI
    Oct 29, 2014 at 14:09
  • Make sure the siteUrl and the listname are updated. Its most likely you have provided wrong siteURL. Oct 29, 2014 at 14:10
  • I had changed site URL and listname
    – AnnaCICI
    Oct 29, 2014 at 14:11
  • What is the exact error message. Inside onQuerySucceeded put an alert statement alert(nextSequence); Oct 29, 2014 at 14:15
  • Hi, it works. Because I modified formula with InfoPath. When I created a new sandard list, it works. But the first item always no value in Sequence. And other items always display 000002 in Sequence.
    – AnnaCICI
    Oct 29, 2014 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.