Determining if a SharePoint 2010 list exists through jquery


Below is a javascript jquery function to determine if a specific list exists within the current site.
This was the first step in the overall project, but this is useful on its own.  The addFields() function will be described later.  The variable webURL is valuable because it constructs the site URL regardless of the page URL.





<p id="hits"></p>
<script type="text/javascript" src="/Style Library/js/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="/Style Library/js/jquery.SPServices.min.js"></script>
<script type="text/javascript">
webURL = window.location.protocol + '//' + window.location.host + _spPageContextInfo.siteServerRelativeUrl;
var myList="MarksHitCounter"; // the name of your list

function addListIfItDoesntExist()
{
 var listExists=false;
 $().SPServices
 ({
  operation: "GetListCollection",
    async: false,
    completefunc: function (xData, Status)
    {
      // go through the result
      $(xData.responseXML).find('List').each
      (function()
       {
          if ($(this).attr("Title") == myList)
          {
           listExists=true; return false
          }
         }
        )
       } 
    })
    // if the list doesn't exist
    if (!listExists)
    {
//     alert("no list");
      // see the MSDN documentation available from the SPService website
       $().SPServices
       ({
        operation: "AddList",
        async: false,
        listName:myList,
        description:"Hit Reports",
        templateID:100
     })
     // add fields
     addFields();
    }
    else
    {
//    alert("list exists");
    }

}

Comments

Popular posts from this blog

Setting SharePoint announcements to auto delete after expiration

Updating a single field in a SharePoint List using Power Automate Flows

SharePoint driven rich text dashboard using jqueryui. (JQuery file)