Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Duplicate items from Switch Statements Javascipt SharePoint REST.

$
0
0

i was wondering if someone could help with checking for duplicate results from switch statement. i trying to retrieve items from a sharepoint blog site and since the categories field takes multiple values. i am receiving duplicate values. What i would want is to receive a single value for each item. In my case i am receiving the same item more than once for each category that it s assigned.

for instance if an item is assigned the category "Well,Application Support and General" it shows up 3 times . Is there a way limit the result to 1 item and the categories are then laid out in the span tag.

or is my Logic completely wrong? 

$.ajax({
            url: _spPageContextInfo.webAbsoluteUrl + "/Blog/_api/web/lists/getbytitle('Posts')/items?$select=Title,ID,PostCategory/Title,PostCategory/ID&$expand=PostCategory",
            type: "GET",
            headers: { "ACCEPT": "application/json;odata=verbose" },
            success: function(data) {
		console.log(data);
		var items = "";
		var posturlLink= "";
		var posturl = _spPageContextInfo.webAbsoluteUrl + "/Blog/Lists/Posts/Post.aspx?ID=";
		var categoryurl = _spPageContextInfo.webAbsoluteUrl + "/Blog/Lists/Categories/Category.aspx?CategoryId=";
		//console.log(categoryurl);
        $.each(data.d.results, function(key, value) {
		var blogCategoryurl;
		var blogPosturl = posturl + value.ID;
		posturlLink = '<li>' + '<a href="' +blogPosturl+ '" >' + value.Title + '</a>' + '<p><span>' +blogCategoryurl+ '</span></p>' + '</li>';
		console.log (posturlLink);
                //console.log ("Title:"+value.Title);


		 $.each(value.PostCategory.results, function(k, v) {

			switch(v.Title) {
    				case 'General':
        				var blogCategoryurl = categoryurl + v.ID;
					posturlLink = '<li>' + '<a href="' +blogPosturl +'"'+ 'target="_blank"' + '" >' + value.Title + '</a>' + '<p><span>' +'<a href="' +blogCategoryurl+ '" >' + v.Title + '</a>' + '</span></p>' + '</li>';
					console.log(v.ID);
        				break;
    				case 'Application/IT Support':
        				blogCategoryurl = categoryurl + v.ID;
					posturlLink = '<li>' + '<a href="' +blogPosturl +'"'+ 'target="_blank"' + '" >' + value.Title + '</a>' + '<p><span>' +'<a href="' +blogCategoryurl+ '" >' + v.Title + '</a>' + '</span></p>' + '</li>';
        				break;
				case 'Exploration and Geophysics':
        				blogCategoryurl = categoryurl + v.ID;
					posturlLink = '<li>' + '<a href="' +blogPosturl +'"'+ 'target="_blank"' + '" >' + value.Title + '</a>' + '<p><span>' +'<a href="' +blogCategoryurl+ '" >' + v.Title + '</a>' + '</span></p>' + '</li>';
        				break;
    				case 'Petrophysics and Geomechanics':
        				blogCategoryurl = categoryurl + v.ID;
					posturlLink = '<li>' + '<a href="' +blogPosturl +'"'+ 'target="_blank"' + '" >' + value.Title + '</a>' + '<p><span>' +'<a href="' +blogCategoryurl+ '" >' + v.Title + '</a>' + '</span></p>' + '</li>';
        				break;
    				case 'Production Geoscience':
        				blogCategoryurl = categoryurl + v.ID;
					posturlLink = '<li>' + '<a href="' +blogPosturl +'"'+ 'target="_blank"' + '" >' + value.Title + '</a>' + '<p><span>' +'<a href="' +blogCategoryurl+ '" >' + v.Title + '</a>' + '</span></p>' + '</li>';
        				break;
    				case 'Reservoir Engineering':
        				blogCategoryurl = categoryurl + v.ID;
					posturlLink = '<li>' + '<a href="' +blogPosturl +'"'+ 'target="_blank"' + '" >' + value.Title + '</a>' + '<p><span>' +'<a href="' +blogCategoryurl+ '" >' + v.Title + '</a>' + '</span></p>' + '</li>';
        				break;
				case 'Technical Data Management':
        				blogCategoryurl = categoryurl + v.ID;
					posturlLink = '<li>' + '<a href="' +blogPosturl +'"'+ 'target="_blank"' + '" >' + value.Title + '</a>' + '<p><span>' +'<a href="' +blogCategoryurl+ '" >' + v.Title + '</a>' + '</span></p>' + '</li>';
        				break;
    				case 'Wells':
        				blogCategoryurl = categoryurl + v.ID;
					posturlLink = '<li>' + '<a href="' +blogPosturl +'"'+ 'target="_blank"' + '" >' + value.Title + '</a>' + '<p><span>' +'<a href="' +blogCategoryurl+ '" >' + v.Title + '</a>' + '</span></p>' + '</li>';
        				break;

				}
			 $("#announcementsItems").append(posturlLink);

		});

        });

thanks


Viewing all articles
Browse latest Browse all 7589

Trending Articles



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