-1

I need to fetch the SharePoint field values using SPServices in a EditForm.aspx in my SP 2010 Listform. As far as I know, some examples, one of them - I tried below - used to fetch the current logged in user. I didn't see, how to get the field values from the existing list item using this API.

I don't have Visual Studio so, need to depend a lot on SP D 2010 and content editor web parts.

                       <script language="javascript" 
           src="/project/EOVHYSZ1/EASAssetsLib/prototype.js" 
            type="text/javascript"></script>
           <script language="javascript" 
            src="/project/EOVHYSZ1/EASAssetsLib/jquery-1.6.0.min.js" 
            type="text/javascript"></script>
              <script language="javascript" 
               src="/project/EOVHYSZ1/EASAssetsLib/SPUtility.js" 
               type="text/javascript"></script>
              <script language="javascript" 
                src="/project/EOVHYSZ1/EASAssetsLib/jquery.SPServices-
                2014.01.min.js" type="text/javascript"></script>
                 <script type="text/javascript" language="javascript">
                var CSGANCHORNames= $().SPServices.SPGetCurrentUser();  /  
                alert(CSGANCHORNames);
samolpp2
  • 3,530
  • 5
  • 54
  • 97

1 Answers1

1

You can do this easily with the built in JSOM or REST APIs.

Reference this within a content editor:

<script>
    SP.SOD.executeOrDelayUntilScriptLoaded(function() {
        JSRequest.EnsureSetup();
        var itemId = JSRequest.QueryString["ID"];
        var listId = SP.PageContextInfo.get_pageListId();
        var oCtx = SP.ClientContext.get_current();
        var oList = oCtx.get_web().get_lists().getById(listId);
        var oItem = oList.getItemById(parseInt(itemId, 10));
        oCtx.load(oItem);
        oCtx.executeQueryAsync(function() {
            console.log(oItem.get_fieldValues());
            // do other work with the fieldValues here...
        }, function(sender, args) {
            // failure
            console.log(args.get_message());
        });
    }, "SP.js");
</script>

But this just fetches the field values for the item, it doesn't garauntee they're on the edit form or let you make changes that will reflect on the edit form -- you would need to do some HTML manipulation to change items on the form or use whichever API to make changes to the item in the background.

John-M
  • 5,930
  • 3
  • 18
  • 36
  • Thank you John-M, will the jquery document.ready function will co-exist with the sp.sod.executeordelay function? can I use both within the SP Designer ? – samolpp2 Oct 29 '15 at 06:30
  • 1
    Yes you can use both but you may run into issues if you try using SharePoint library code inside of a jQuery ready -- best practice is to always use executeFunc or executeOrDelayUntilScriptLoaded; here is a good answer related to that: http://sharepoint.stackexchange.com/questions/58503/sp-sod-how-to-use-correctly – John-M Oct 29 '15 at 13:02