1

I want to create one SP dialogue box at home page which load the new item form of list or survey if current login user is not created any item previously.

Is there any way to do that ?

I tried using query but it takes time to load data. My Code is as below :

<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(init,'sp.js');
var context;
var web;
var user;
var siteUrl = _spPageContextInfo.siteServerRelativeUrl ;
//var userId= _spPageContextInfo.userId;
var userId= 123;
var flag=true;
//var options = { title: "Announcements - New Item",url:"../Lists/Announcements/NewForm.aspx"};
var options = { title: "Announcements - New Item",url:"http://webtest:3333/Lists/test/NewForm.aspx"};

function init() {
context = SP.ClientContext.get_current();
web = context.get_web();
//alert(flag);
getCurrentUser();
retrieveListItems();

}

function getCurrentUser() {
user = web.get_currentUser();
context.load(user);
context.executeQueryAsync(onGetUserNameSucces, onGetUserNameFail);
}
function onGetUserNameSucces() {
// TODO: implement your own succes handler
    //alert(user.get_title());
}
function onGetUserNameFail(sender, args) {
// TODO: implement your own fail handler and do some logging
}

function retrieveListItems() {
      var oList = context.get_web().get_lists().getByTitle("test");
     var camlQuery = new SP.CamlQuery();

     //context.load(user);
     //alert(userId);

camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name=\'Author\' /><Value Type=\'User\'>'+ user.get_title() +'</Value></Eq></Where></Query></View>');
    this.collListItem = oList.getItems(camlQuery); 
    context.load(collListItem);
    context.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded),Function.createDelegate(this, this.onQueryFailed));


}

function onQuerySucceeded(sender, args) {       
    //alert( _spPageContextInfo.userId);

    var listItemInfo = '';
    var listItemEnumerator = collListItem.getEnumerator();


    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        listItemInfo += '\nID: ' + oListItem.get_id() + 
            '\nTitle: ' + oListItem.get_item('Title');
    }
    alert(listItemInfo.toString());
    if(listItemInfo==null){
//      flag='false'; 
     SP.UI.ModalDialog.showModalDialog(options);

          }
     //alert(flag);
       }

function onQueryFailed(sender, args) {
    alert('Error: ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>

I cant get result in the data

3

The primary issue that is present here is two asynchronous calls that have been made. This causes 2 threads that execute independently and thus the required function is fails to load.

In order to get the current logged in user use the following code:

var currentLoggedinUser = _spPageContextInfo.userLoginName;

This function will help you to get rid of the getcurrentuser() function.

Whenever you have several asynchronous calls to be made, it advisable to make nested calls rather then independant calls as shown below:

function function1(){
    //Your logic
    context.executeQueryAsync(onQuerySucceeded1,onQueryFailed);
}
function onQuerySucceeded1(sender, args) {  
      //success function1

      //Call the second function here
      function2();
      context.executeQueryAsync(onQuerySucceeded2,onQueryFailed);
}
function onQuerySucceeded2(sender, args) {  
      //success function2         
}
function onQueryFailed(sender, args) {  
      console.log("Error");
}

Use the code given below instead of your code and it shall solve your problem.

<script>
ExecuteOrDelayUntilScriptLoaded(init,'sp.js');
var context;
var web;
var user;
var siteUrl = _spPageContextInfo.siteServerRelativeUrl ;
//var userId= _spPageContextInfo.userId; 
var userId= 123;
var flag=true; 
var options = { title: "Announcements - New      Item",url:"http://webtest:3333/Lists/test/NewForm.aspx"};

function init() {
   context = SP.ClientContext.get_current();
   web = context.get_web();
   retrieveListItems();
}

function retrieveListItems() {
   var oList = context.get_web().get_lists().getByTitle("test");
   var camlQuery = new SP.CamlQuery();
   camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name=\'Author\'      /><Value Type=\'User\'>'+ user.get_title() +'</Value></Eq></Where></Query>  </View>');
   this.collListItem = oList.getItems(camlQuery); 
   context.load(collListItem);
   context.executeQueryAsync(onQuerySucceeded,onQueryFailed);
}

function onQuerySucceeded(sender, args) {           
   var listItemInfo = '';
   var listItemEnumerator = collListItem.getEnumerator();

   while (listItemEnumerator.moveNext()) {
       var oListItem = listItemEnumerator.get_current();
       listItemInfo += '\nID: ' + oListItem.get_id() + 
        '\nTitle: ' + oListItem.get_item('Title');
    }
      alert(listItemInfo.toString());
      if(listItemInfo==null){
       SP.UI.ModalDialog.showModalDialog(options);
     }
   }

   function onQueryFailed(sender, args) {
      alert('Error: ' + args.get_message() + '\n' + args.get_stackTrace());
   }
   </script>
  • Thanks mansi my issue resolved.the issue is as you stated of async function – akj Apr 19 '16 at 10:52
  • Could you please mark this as an answer then? – Mancy Desaee Apr 19 '16 at 11:16
0

No need for jQuery or JSOM code.

Use SharePoint for what it can do:

  1. Tell SharePoint what you want
  2. Create a View by User entry
  3. Add the View to the New Form
  4. Hide the New Form when User has entries in the View

Detailed description at: Limit only one entry per day per user in sharepoint list

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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