locked
Retrieve List Item ID RRS feed

  • Question

  • Hello,

    I want to share (share Facebook) my Custom List Item. I used below code:

    <a href="http://www.facebook.com/sharer/sharer.php?u=http://duit.matawang.com.my/ms/gallery/duit/Lists/Posts/Post.aspx?&t=Description" target="_blank">Facebook</a>

    How to get list item Id to insert in above URL (bold). 

    Thank You

    Tuesday, July 26, 2016 3:22 PM

Answers

  • Hi,

    According to your description, my understanding is that you want to get item ID and insert it to URL you apply.

    We can get item ID with JSOM via Script Editor Web Part and the code as below.

    <script src="http://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
        <script type="text/javascript">
            $(document).ready(function () {
                ExecuteOrDelayUntilScriptLoaded(getItemID, "sp.js");
            });
    
            var allItems;
            var ids = [];
    
            function getItemID() {
                var context = new SP.ClientContext.get_current();
                var web = context.get_web();
                var list = web.get_lists().getByTitle('Your List Title');
                var query = SP.CamlQuery.createAllItemsQuery();
                allItems = list.getItems(query);
                context.load(allItems);
                context.executeQueryAsync(successGetListItemId);
    
                function successGetListItemId() {
    
                    var Itemid = "";
                    var s = "";
                    var ListEnumerator = allItems.getEnumerator();
                    while (ListEnumerator.moveNext()) {
                        var currentItem = ListEnumerator.get_current();
                        Itemid = currentItem.get_item('ID');
                        ids.push(Itemid);
                    }
                    var t = document.getElementById("{D69D2435-2907-4754-9087-87867EA4088A}-{6EC179BD-FB2C-40C8-A772-FEC68F867DD6}");
                    for (var i = 1; i < t.rows.length; i++) {
                        s = "<a href='http://www.facebook.com/sharer/sharer.php?u=http://duit.matawang.com.my/ms/gallery/duit/Lists/Posts/Post.aspx?ID=" + ids[i - 1] + "&t=Description' target='blank'>Share</a>"
    
                        t.rows[i].cells[9].innerHTML = s;
                    }
                }
            }
        </script>
    

    Here is a picture for the final rendering:

    Best Regards,

    Andy Wu



    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    Wednesday, July 27, 2016 10:54 AM

All replies

  • You need to try like below

    http://duit.matawang.com.my/ms/gallery/duit/Lists/Posts/Post.aspx?ID=1


    ---
    Rajesh
    rjesh.com| @rjesh
    You don't need to buy me a beer, if helpful just smile, vote, and mark it as answer.

    Tuesday, July 26, 2016 4:54 PM
  • Hi,

    According to your description, my understanding is that you want to get item ID and insert it to URL you apply.

    We can get item ID with JSOM via Script Editor Web Part and the code as below.

    <script src="http://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
        <script type="text/javascript">
            $(document).ready(function () {
                ExecuteOrDelayUntilScriptLoaded(getItemID, "sp.js");
            });
    
            var allItems;
            var ids = [];
    
            function getItemID() {
                var context = new SP.ClientContext.get_current();
                var web = context.get_web();
                var list = web.get_lists().getByTitle('Your List Title');
                var query = SP.CamlQuery.createAllItemsQuery();
                allItems = list.getItems(query);
                context.load(allItems);
                context.executeQueryAsync(successGetListItemId);
    
                function successGetListItemId() {
    
                    var Itemid = "";
                    var s = "";
                    var ListEnumerator = allItems.getEnumerator();
                    while (ListEnumerator.moveNext()) {
                        var currentItem = ListEnumerator.get_current();
                        Itemid = currentItem.get_item('ID');
                        ids.push(Itemid);
                    }
                    var t = document.getElementById("{D69D2435-2907-4754-9087-87867EA4088A}-{6EC179BD-FB2C-40C8-A772-FEC68F867DD6}");
                    for (var i = 1; i < t.rows.length; i++) {
                        s = "<a href='http://www.facebook.com/sharer/sharer.php?u=http://duit.matawang.com.my/ms/gallery/duit/Lists/Posts/Post.aspx?ID=" + ids[i - 1] + "&t=Description' target='blank'>Share</a>"
    
                        t.rows[i].cells[9].innerHTML = s;
                    }
                }
            }
        </script>
    

    Here is a picture for the final rendering:

    Best Regards,

    Andy Wu



    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    Wednesday, July 27, 2016 10:54 AM
  • Hi

    this is how i recolved it

    https://www.facebook.com/dialog/feed?%20%20app_id=1680171872234988%20&display=popup&caption=Wallpaper%20of%20the%20Week%2030/2016%20&picture=HTTP://WWW.ROMEODONCA.RO/PL_WALLPAPER/_W/201607185188_jpg.jpg%20&link=http://www.romeodonca.ro/sitepages/photos/wallpaper.aspx?idul=240%20&description=Pink%20lily%20&redirect_uri=http://www.romeodonca.ro/sitepages/photos/wallpaper.aspx?idul=240

    for URL

    http://www.romeodonca.ro/sitepages/photos/wallpaper.aspx?idul=240

     This code is created using javascript code -

    Very important to create an app id on facebook - in my case , the app id is

    app_id=1680171872234988

    which is linked to site's  QFDN

    Check this facebook link to create your own app id

    https://developers.facebook.com/docs/apps/register




    Romeo Donca, Orange Romania (MCSE, MCITP, CCNA) Please Mark As Answer if my post solves your problem or Vote As Helpful if the post has been helpful for you.

    Wednesday, July 27, 2016 12:04 PM