Friday, February 17, 2017

Dynamic SiteURL & RestURL call with X-RequestDigest Sharepoint


siteUrl = _spPageContextInfo.webAbsoluteUrl;

var restUrlnew = siteUrl + "/_api/web/lists/getbytitle('" + priviewList + "')/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) {

Change DATE Format DDMMYYYY OR MMDDYY


Function to get DD MM YYYY  OR MMDDYY


function formattedDate(date) {
    var d = new Date(date || Date.now()),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
   year = d.getFullYear();
   if (month.length < 2) month = '0' + month;
   if (day.length < 2) day = '0' + day;
   return [day,month,year].join('/');
}

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);
}
---------------------




Update or edit status filed in JIRA issue

the status field is a special case and can't be updated directly, which is the second problem here. Changing a status in Jira is called ...