Friday, February 17, 2017

Updating Sp list onclick of download or Open button

On click of download and Open button in document preview



//onClick of open button
        $('.preview-template').on('click', '.open-test', function (ev) {
            //ev.preventDefault();          
            updateToDownloadDetails();
            //window.location.href=serviceURL;        
        });

        //ONCLICK OF download
        $('.preview-template').on('click', '.download-test', function (ev) {
            //ev.preventDefault();
            updateToDownloadDetails();
        });
       

--------------------------

function updateToDownloadDetails() {
    try {
        var DownloadDate = new Date();
        var DownloadBy = CurrentUserID;
        DownloadCount = 1;
        var itemID = rowID;
        var listItem = ["Title", downloadURL, DownloadDate, DownloadBy, DownloadCount, olistName];
        clientContext = new SP.ClientContext(siteUrl);
        oList = clientContext.get_web().get_lists().getByTitle(downloadListName);
        var itemCreateInfo = new SP.ListItemCreationInformation();
        oListItem = oList.addItem(itemCreateInfo);
        oListItem.set_item('Title', "New Title");
        oListItem.set_item('DocumentUrl', listItem[1]);
        oListItem.set_item('DownloadDate', listItem[2]);
        oListItem.set_item('DownloadBy', listItem[3]);
        oListItem.set_item('DownloadCount', listItem[4]);
        oListItem.set_item('SourceLibrary', listItem[5]);
        oListItem.update();
        clientContext.executeQueryAsync
       (
           Function.createDelegate(this, function () { onSucceededDownloadUpdates(olistName,rowID); }),
           Function.createDelegate(this, this.onSucceededDownloadUpdates(olistName,rowID))
       );  
      }
       
        catch (err) {
            console.log(err.message);
        }
        }

function onSucceededDownloadUpdates(priviewList,itemid) {
GetDownloadCount(priviewList,itemid)
    console.log('Item updated!');
}

function onFailedDownloadUpdates(sender, args) {
    console.log('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    return false;
}

function GetDownloadCount(olistName,itemid){
 try {
           var restUrlnew = siteUrl + "/_api/web/lists/getbytitle('" + olistName+ "')/items(" + itemid+ ")?$select=Downloads";
            $.ajax({
                url: restUrlnew,
                method: "GET",
       headers: { "Accept": "application/json; odata=verbose",
       "X-RequestDigest": jQuery("#__REQUESTDIGEST").val()
        },                
                async: false,
                success: function (data) {
                var count=data.d.Downloads;
                UpdateListData(olistName,itemid,count);
                     }, //succss function
                error: function (data) {
                    alert("error");
                }
            });
        }        
        catch (err) {
            console.log(err.message);
        }            
}

function UpdateListData(ListName,itemId,cntdownloads) {
    try{
        var oList = clientContext.get_web().get_lists().getByTitle(ListName);
        var oListItem = oList.getItemById(itemId);
        var Count;
        Count=cntdownloads+1;
        oListItem.set_item('Downloads', Count);
        oListItem.update();
        clientContext.load(oListItem);
        clientContext.executeQueryAsync
           (
               Function.createDelegate(this, function () { onQuerySucceededUpdateList(); }),
               Function.createDelegate(this, this.onQueryFailed)
           );
    }
    catch(err){
        console.log(err.message);
    }
}

function onQuerySucceededUpdateList() {
    console.log('Item updated!');
     location.reload(false);
}
---------------------




No comments:

Post a Comment

Building Secure Azure AI Foundry Agents with Managed Identity

  The credential problem nobody wants to own Every team that connects an Azure AI Foundry agent to Microsoft Graph, SharePoint, Outlook, or...