0
votes

I've looked at the solutions for auto numbering in a SharePoint list, but here is my problem. I don't have SPD, Infopath, visual studio. I only have the HTML webpart. The company I work for does not trust people with these tools, so I have to make do. Anyway, I have imported a spreadsheet as a Sharepoint list. I want to continue with auto numbering from that list. The last number was 3500. I want to enter a new item in a form where the next number automatically appears (3501 and so on). Please note that answers pertaining to SPD or any other tools (less java script - I can use that I guess) won't work. I can't even use workflows as I only have the vanilla workflows, not those as part of SPD.

Thankyou

1

1 Answers

0
votes

Alright, well the good news is that you can make something work with JavaScript, but it won't be pretty.

You can edit the "New Item" form for your SharePoint list and add a content editor web part below the default form web part.

Edit the content editor web part properties and set its "content link" property to be a text file in a document library on your site.

In that text file, you can embed whatever HTML and JavaScript you want... in this case, we'll add some JavaScript that uses the SharePoint client side object model (CSOM) to query the list and get the largest number in your number column. Then we'll override the PreSaveAction() function (which triggers whenever someone clicks "save"), and have it set the item's number to that value plus one before saving.

In the code below, replace "Example List" with the title of your list, and "MyAutonumberColumn" with the internal name of your column with the autonumbering.

<script>
window.MyNamespace = new Object(null);
var my = window.MyNamespace;
my.listName = "Example List";
my.columnInternalName = "MyAutonumberColumn";

ExecuteOrDelayUntilScriptLoaded(GetAutoNumber,"SP.JS");
function PreSaveAction(){
    var fieldDictionary = new Object(null);
    var fields = document.querySelectorAll("td.ms-formbody");
    for (var i=0, len = fields.length; i < len; i++){
        var start = fields[i].innerHTML.indexOf("FieldInternalName=");
        if(start >= 0){
            start += 19;
            var internalName = fields[i].innerHTML.substring(start);
            internalName = internalName.substring(0,internalName.indexOf('"'));
            fieldDictionary[internalName] = fields[i];
        }
    }
    if(fieldDictionary[my.internalColumnName]){
        var element = fieldDictionary[my.internalColumnName].querySelector("select, textarea, input");
        element.value = my.autoNumber;
        return true;
    }
}
function GetAutoNumber(){
    var my = window.MyNamespace;
    var clientContext = new SP.ClientContext();
    var list = clientContext.get_web().get_lists().getByTitle("my.listName");
    var camlQuery = new SP.CamlQuery();
    // use a CAML query to retrieve a set of list items:
    camlQuery.set_viewXml(
        '<View><Query>'+
        '<OrderBy>'+
            '<FieldRef Name=\'"+my.columnInternalName+"\' Ascending='FALSE' />'+
        '</OrderBy>'+
        '</Query><RowLimit>1</RowLimit></View>');
    var items = list.getItems(camlQuery);
    clientContext.load(items);
    clientContext.executeQueryAsync(
        /* code to run on success: */
        Function.createDelegate(this,
            function(){
                var item, itemEnumerator = items.getEnumerator();
                while(itemEnumerator.moveNext()){
                    item = itemEnumerator.get_current();
                    var number = item.get_item(my.columnInternalName);
                    my.autoNumber = +(number) + 1;
                }
            }
        ),
        /* code to run on error: */
        Function.createDelegate(this,
            function(sender,args){
                alert(args.get_message());
            }
        )
    );
    </script>

There is an obvious drawback to this approach: if two people start creating items at the same time, they'll be created with the same number. You could always go in and edit those items to give them different numbers as needed, which might be fine if this list isn't being frequently updated with new items created by multiple people.