Add fields to a list in SharePoint 2010 with JQuery and CAML Query

The jquery javascript code below will add a field, named "Page" to the list in the variable myList.
I use CAML Query to create the field.
Note: the code snip is missing the function call.

<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 addFields() 
{
  var nfields = " \
  <Fields> \
   <Method ID='1'> \
    <Field Type='Text' DisplayName='Page' ResultType='Text'></Field> \
   </Method> \
   </Fields>";
  $().SPServices
  ({
        operation: 'UpdateList',
        listName: myList,
        newFields: nfields,
        completefunc: function(xData, Status) 
        {
            tp1 = xData.responseText;
//            alert(tp1);
            tp4 = tp1.indexOf("errorstring");
            if (tp4 < 0) 
            {
//                alert("Fields created!");
            }
            else 
            {
                // Error creating fields!
                alert("Error creating fields!");
            }
        }
    })
}

Comments

Popular posts from this blog

Security Considerations When Using SharePoint Online

Setting SharePoint announcements to auto delete after expiration

Unexpected Integers where dates should be with Excel Online (Business) connector in SharePoint Online