3

I have created one custom form and insert data into one list.I want to check before data insert into the list if the data exists in list or not .

2
  • 2
    On which column value you want to have a check? Like there shouldn't be another item if an item with same title exists?
    – Asad Refai
    Commented Oct 9, 2015 at 6:10
  • for ex:group=epm,user=xyz and save this data into my list,again if i given same values it should show already inserted
    – chakri
    Commented Oct 9, 2015 at 6:26

3 Answers 3

0
var clientContext = SP.ClientContext.get_current();     
list = clientContext.get_web().get_lists().getByTitle('ListName');           
var skillcamlQuery = new SP.CamlQuery();        
//skillcamlQuery.set_viewXml("<View><Query><Where><And><Eq><FieldRef Name='Title' /><Value Type='Text'>"+VariableName+"</Value></Eq><Eq><FieldRef Name='BusinessDomain' /><Value Type='Text'>"+VariableName+"</Value></Eq></And></Where></Query></View>");

skillcamlQuery.set_viewXml("<View><Query><Where><Eq><FieldRef Name='Title' /><Value Type='Text'>"+VariableName+"</Value></Eq></Where></Query></View>");
this.skillcollListItem = list.getItems(skillcamlQuery);
//clientContext.load(list);         
clientContext.load(skillcollListItem);

var callback = clientContext.executeQueryAsync(Function.createDelegate(this, function(){ _returnParam= onValidateClientSucceeded();}),Function.createDelegate(this, function(){ _returnParam = onValidateClientFailed();}));            







function onValidateClientSucceeded(sender, args)
  {     

        var flg = 0;


        if(skillcollListItem.get_count() >= 1)
        {
            flg = 1;            
        }
        else
        {


        }


  }

     function onValidateClientFailed(sender, args)
     { 
         alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
         return false;
     }
4
  • 1
    what is the use of above code?
    – chakri
    Commented Oct 9, 2015 at 7:33
  • Above code ensures if the data exists in the list or not. If I am not wrong that's what you have asked in question. Commented Oct 9, 2015 at 9:51
  • I tried above code its not working .
    – chakri
    Commented Oct 9, 2015 at 10:50
  • How I mean what did you did? It is a working code that I have implemented in one of my solutions. Commented Oct 9, 2015 at 11:38
0

Hello I had the exact same requirement a few days ago. I had to check if a value in one field existed in an item in another list and do this validation when user is saving the form. So the field in this example is eventdate and the field in the second list is time2 also i run this validation when a value in the first form is yes this field is demofield. Basically what this code does is if a demo field is yes it goes and checks in a list called demotrainingroom2 if time2 is equal to eventdate on every time. For this you need jquery and SPUtility.js. Check this fiddle

0

try the below code hope this will be helpfull.

 var hostWebUrl = decodeURIComponent(getQueryStringParameter('SPHostUrl'));//getting host URL

    var appWebUrl = decodeURIComponent(getQueryStringParameter('SPAppWebUrl'));//getting hosted app web URL

    var ctx = new SP.ClientContext(appWebUrl);//Get the SharePoint Context object based upon the URL  



    var appCtxSite = new SP.AppContextSite(ctx, hostWebUrl);

    var web = appCtxSite.get_web(); //Get the Site   

    var MyList = web.get_lists().getByTitle("MyList");// hosted list

    function getQueryStringParameter(urlParameterKey) {
        var params = document.URL.split('?')[1].split('&');
        var strParams = '';
        for (var i = 0; i < params.length; i = i + 1) {
            var singleParam = params[i].split('=');
            if (singleParam[0] == urlParameterKey)
                return decodeURIComponent(singleParam[1]);
        }
    }

    function getItemCount()
    {
     var query = new SP.CamlQuery();
     var items = MyList.getItems(query);
            ctx.load(items);
        ctx.executeQueryAsync(
            Function.createDelegate(this, function () {
              var count = items.get_count();
               -
               -
               -(your code will go here according to count)
               -
               -
               -
            }),
            Function.createDelegate(this, function () {
                alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
            })
            );
    }
    $(document).ready(function () {
    getItemCount();
    });

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.