Quantcast
Channel: jQuery Library for SharePoint Web Services
Viewing all articles
Browse latest Browse all 6517

New Post: Write to list with anonymous access

$
0
0
iOnline247 wrote:
@spevilgenius

There was a reason I said: "demo must use an OOTB SharePoint API and easily reproducible". I don't really care what you use other than Javascript and an OOTB SharePoint API. With that said, if you can get it to work in JSOM/CSOM whatever it's called, PROVE IT. ;-)

Where's my demo script or better yet a demo page?

Cheers,
Matthew
Since I was the one who started this discussion, I figured I'd contribute to what I've recently discovered in the past few weeks. I would have posted this sooner, but completely forgot about this post.

Spevilgenius is correct you can create, update, delete a list item with anonymous access through the Javascript Client Side Object Model and it is OOTB. As long as the anonymous user has rights to create, edit and delete to the list, it works. The only thing that does not work, for some strange reason is to View list items (getItems();). Although I've used SPServices GetListItems function to get around this with conjunction with a CSOM update function.

Below are a few links that I've referenced to get this to work.

How to: Create, Update, and Delete List Items Using JavaScript
http://msdn.microsoft.com/en-us/library/hh185011.aspx

Using SharePoint CSOM in HTML5 apps
http://allthatjs.com/2012/04/03/using-sharepoint-csom-in-html5-apps/

This is an example that I've used on my site to test it out. Just replace the site, list and internal names and it should be good to go. This is the basic code to get this working:
<<!DOCTYPE html>
<%@ Page language="C#" %>
<%@ Register Tagprefix="SharePoint" 
     Namespace="Microsoft.SharePoint.WebControls" 
     Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<html xmlns:mso="urn:schemas-microsoft-com:office:office" xmlns:msdt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882">
<head>

<!-- the following 5 js files are required to use CSOM -->
    <script src="/_layouts/1033/init.js"></script>
    <script src="/_layouts/MicrosoftAjax.js"></script>
    <script src="/_layouts/sp.core.js"></script>
    <script src="/_layouts/sp.runtime.js"></script>
    <script src="/_layouts/sp.js"></script>
  

 
    <!-- include your app code -->
    
 
</head>
<body>
    <SharePoint:FormDigest ID="FormDigest1" runat="server">
    </SharePoint:FormDigest>
</body>
<script type="text/javascript">
  

  // define a ClientContext for the specified SP site
var ctx = new SP.ClientContext("/alumni/");
 
// attach a onRequestFailed function to the client context.
ctx.add_requestFailed(function (sender, args) {
    alert('Request failed: ' + args.get_message());
});
 
function example () {

// GETS SITE FIELD INFO
    var web = ctx.get_web();
 
    ctx.load(web);
    ctx.executeQueryAsync(function () {
        alert("Title: " + web.get_title());
        alert("Description: " + web.get_description());
    });


/// GET LIST ITEMS DOES NOT WORK WITH ANON
    /*  var items = ctx
            .get_web()
            .get_lists()
            .getByTitle('Ad Content')
            .getItems('');
 
    var contacts = ctx.loadQuery(items);
 
    ctx.executeQueryAsync(function () {
        contacts.forEach(function (contact) {
       console.log("Pages: ", contact.get_fieldValues())
        })
    })*/
    
 // CREATE LIST ITEM   
    var list = ctx
            .get_web()
            .get_lists()
            .getByTitle('Ad Content');
    
      // create a new item on the list
    var contact = list.addItem(new SP.ListItemCreationInformation());
    // You may even leave out the ListItemCreationInformation, 
    // if you don't set any of its properties:
    // var contact = list.addItem();
     
    contact.set_item("Title", "TestAnon"); // 'Title' is the internal name for 'Last Name'
    contact.set_item("Ad_x0020_Order", "4");
 
    // ensure that contact is saved during the query
    contact.update();
 
 
    ctx.executeQueryAsync(function () {
        console.log("Id of new contact: ", contact.get_id()); 
    })
 //UPDATE LIST ITEM

    // ID OF LIST ITEM
     var id = 15,
        item = ctx
            .get_web()
            .get_lists()
            .getByTitle('Ad Content')
            .getItemById(id);
 
    // Change business phone number.
    // Internal name is WorkPhone
    item.set_item("Ad_x0020_Order", "1");
 
    // ensure that contact is saved during the query
    item.update();
 
    ctx.executeQueryAsync(function () {
        console.log("New value: ", item.get_item("Ad_x0020_Order"));
    })
    
};

 
window.onload = example;

</script>
</html>

Viewing all articles
Browse latest Browse all 6517

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>