Pages

Search This Blog

Showing posts with label SharePoint 2013. Show all posts
Showing posts with label SharePoint 2013. Show all posts

Monday, August 17, 2015

SharePoint Increase search result items limit of Content Search Web Part by using javascript




SharePoint Content Search Web Part allow to show search results upto 50 Items per page.
Out Of The Box you cant allow it to show more than 50 Items per page.

But there may be situations with business needs where you need to override this behavior.
So here is javascript snippet which overrides this behavior and allows more than 50 items in search result.

How to use :

1. Add a Script Editor web part on the same page where Content Search web part is present.
2. Paste the following script and change the maxItems value as per your needs.
3. The script grabs the 2 SharePoint search objects ContentBySerach and DataProvider and overrides it default value to show more than 50 items.



<script type="text/javascript">

var $ocreate = null, maxItems = 100;

Sys.Application.add_init(function() {
$ocreate = $create; $create = updateResultCountCreate;
});

function updateResultCountCreate(a,b){
var ps = Array.prototype.slice.call(arguments, 0);
if(a == Srch.ContentBySearch) b.numberOfItems = maxItems;
if(a == Srch.DataProvider) b.resultsPerPage = maxItems;
$ocreate.apply(this,ps);
}
</script>

  

Monday, July 27, 2015

Create List View using SharePoint REST API


function createView(ListName, query )
{

var bodyContent = "{ '__metadata': { 'type': 'SP.View' }, 'Title': 'View Name', 'PersonalView': true, 'ViewQuery':'" + query + "' }";

var viewUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/lists/getbytitle('"+ListName+"')/views"

 var executor;  executor = new SP.RequestExecutor(_spPageContextInfo.webAbsoluteUrl);  executor.executeAsync({ url: viewUrl,

method: "POST",

body: bodyContent,

headers: { 

"accept": "application/json;odata=verbose",

"content-type": "application/json;odata=verbose"

},

success: function (results) { alert("View Created.") },

error: function (error) { alert("Error in view creation.") }

});

}

Call this function you have to pass two parameter ListName and query.
ListName : This is name of list in which you want to create view.

Query : You have to pass CAML Query base on which filter is set and your view is created. Like

 '<Where><Eq><FieldRef Name=\"'Title'\" /><Value Type=\"Text\">' + TitleVar +'</Value></Eq</Where>';




You can customize your view but adding more attribute in body.

This code create a personal view because i set 'PersonalView' : true. If you want to create public view than set it to false. 

var bodyContent = "{ '__metadata': { 'type': 'SP.View' }, 'Title': 'View Name', 'PersonalView': true, 'ViewQuery':'" + query + "' }";


For more detail check https://msdn.microsoft.com/en-us/library/office/dn531433.aspx#bk_View

Note : 

1. Please make sure that you have loaded SP.RequestExecutor.js script file. To load script file

var scriptbase = _spPageContextInfo.webAbsoluteUrl+ "/_layouts/15/";

$.getScript(scriptbase + "SP.RequestExecutor.js", yourFunctionName);


2. You have proper permission to create views.

SharePoint CRUD Operations on ListItem PropertyBag

# Initialize the SharePoint PowerShell environment
Add-PSSnapIn "Microsoft.SharePoint.Powershell" -ErrorAction:SilentlyContinue



# Function to perform add , delete , view and update operation on list item properties
# Parameters :
# -- ItemBagKey : Name of property of list item
# -- Operation : Type of operation  "ADD" , "UPDATE" , "DELETE" , "VIEW"
# -- NewValue : Value for the item property 
# -- Item : List item to operate upon 


Function UpdateKey{
      param([string]$ItemBagKey,[string]$Operation,[string]$NewValue,[Microsoft.SharePoint.SPListItem]$item)
      if($Operation -eq "ADD"# If we are adding property to item.
      {
            if($item.Properties.ContainsKey($ItemBagKey))
            {
                  Write-Host "Key already found in item bag." -ForegroundColor Yellow;
                  $item.Properties[$ItemBagKey] = $NewValue;
                  $item.SystemUpdate($false);
                  Write-Host "Key update with new value : "  $NewValue - ForegroundColor Green;
            }
            else
            {
                  Write-Host "Key not found in item bag." -ForegroundColor Yellow;
                  $item.Properties.Add($ItemBagKey,$NewValue);
                  $item.SystemUpdate($false);
                  Write-Host "Key added with value : "  $NewValue -ForegroundColor Green;
            }
      }
      elseif($Operation -eq "DELETE"# If we are deleting property from item.
      {
            if($item.Properties.ContainsKey($ItemBagKey))
            {
                  $item.Properties.Remove($ItemBagKey);
                  $item.SystemUpdate($false);
                  Write-Host "Key removed from item bag : "  $ItemBagKey -ForegroundColor Green;
            }
            else
            {
                  Write-Host "Key not found in item bag to remove." -ForegroundColor Red;
            }
      }
      elseif($Operation -eq "UPDATE"# If we are updating property of item.
      {
            if($item.Properties.ContainsKey($ItemBagKey))
            {
                  $item.Properties[$ItemBagKey] = $NewValue;
                  $item.SystemUpdate($false);
                  Write-Host "Key updated with new value : "  $NewValue -ForegroundColor Green;
            }
            else
            {
                  Write-Host "Key not found in item bag." -ForegroundColor Red;
            }
      }
      elseif($Operation -eq "VIEW"# If we want to view value item property.
      {
            if($item.Properties.ContainsKey($ItemBagKey))
            {
                  Write-Host "Property value : "  $item.Properties[$ItemBagKey] -ForegroundColor Yellow;
            }
            else
            {
                  Write-Host "Key not found in item bag." -ForegroundColor Red;
            }
      }
}

try
{
     
      #Put your site URL in placeholder <siteurl> 
      $web = Get-SPWeb <siteurl>     

      #Put list name in placeholder <listname> 
      $list = $web.Lists["<listname>"


      #If the list is found
      if($list)
      {
  
  #Put list item id in placeholder <itemid> 
  $item = $list.GetItemById(<itemid> );


  #Put Item property key that you want to operate upon in placeholder               <propkey> 
  $_ItemBagKey = "<propkey>"


  #Replace placeholder <OP> with type of operation you want to perfrom it            could be one among four values ( ADD, UPDATE , DELETE , VIEW )
  $_Operation  = "<OP>"  # "ADD" # "UPDATE" # "DELETE" # "VIEW"


       #Replace placeholder <propvalue> with new value that you want to update          if you want to delete or view just leave the value blank.
       $_NewValue   = "<propvalue>"
  

 #call function to perform operation
 UpdateKey $_ItemBagKey $_Operation $_NewValue $item


 #Dispose web object to release resources.
 $web.Dispose()

}
else
{
   Write-Host "List not found." -ForegroundColor Red;
}
}
catch
{
   # Add your exception handling routine here to trap and handle the exception details.
}