Friday, November 17, 2017

Delete list items older than date - SharePoint, PowerShell

You need to delete SharePoint items older than date? It's easy with PowerShell.


Start Sharepoint Management Shell as Administrator

 Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
     
        #Configure target site and list.
     
        $list = $($(Get-SPWeb -Identity 'https://portal/test').Lists['ListName'])
     
        #Index count for list items.
     
        $index = $list.ItemCount
     
        #Index counter for paging.
     
        $page = 0
     
        #Configure how many items to delete per batch.
     
        $pagesize = 3000
     
        #Configure how may seconds to pause between batches.
     
        $sleep = 1
     
        #Turn verbose output on/off
     
        $verbose = $true
     
        While($index -ge 0){
     
        if($verbose){
     
        $("Check item at index $($index).")
     
        }
     
        if($page -lt $pagesize){
     
        try{
     
        if($($list.Items[$index])['DateField'] -lt [DateTime]::Parse("11/7/2017")){
     
        $list.Items[$index].Delete()
     
        write-host "Deleting item at index $($index)." -foregroundcolor "green"
     
        }
     
        }
     
        catch [System.Exception]{
     
        if($verbose){
        $("Skipping item at index $($index).")
     
        }
     
        }
     
        $index--
     
        $page++
     
        }
     
        else{
     
        if($verbose){
     
        $("Sleeping for $($sleep) seconds.")
     
        }
     
        [System.Threading.Thread]::Sleep($sleep * 1000)
     
        $page = 0
     
        }
     
        }

One picture icon with multiple links - SharePoint


How to click on one picture icon and open multiple links?

Add Content Editor Web Part

Edit source

<p><a href="#" onclick="window.open('http://google.com');
    window.open('http://yahoo.com');"><img src="https://projekti/test/pictures/freepik.jpg" style="margin:5px; width:68px; height=67px"></a></p>


 Save









Tuesday, October 24, 2017

Show / Hide fields based on choice field selection using JQuery in SharePoint


As example I have created a simple list with:
  • Name (Single line of text)
  • Insurance (Choice)
  • Family member1 (Single line of text)
  • Family member2 (Single line of text)
  • Family member3 (Single line of text)
If the Insurance field value is Family, fields Family member1, Family member2, Family member are "show". For other options in the Insurance, Family members fields are "hide".


Edit Default New form








Add a Web Part, Media and Content, Contend Editor.
In Edit Source paste next script

<script src="https://projekti/mc-putno_osiguranje/Shared%20Documents/jquery-1.3.2.js" type=text/javascript></script>
 
<script type="text/javascript">
 
// Execute the following JavaScript after the page has fully loaded, when it's ".ready"
$(document).ready(function(){
 
//Define which columns to show/hide by default
  $('nobr:contains("Family member1")').closest('tr').hide();
  $('nobr:contains("Family member2")').closest('tr').hide();
  $('nobr:contains("Family member3")').closest('tr').hide();
//Show/hide columns based on Drop Down Selection
 $("select[title='Insurance']").change(function() {
  if ($("select[title='Insurance']").val() == "Family") {
  $('nobr:contains("Family member1")').closest('tr').show();
  $('nobr:contains("Family member2")').closest('tr').show();
  $('nobr:contains("Family member3")').closest('tr').show()
  } else if($("select[title='Insurance']").val() != "Family"){
  $('nobr:contains("Family member1")').closest('tr').hide();
  $('nobr:contains("Family member2")').closest('tr').hide();
  $('nobr:contains("Family member3")').closest('tr').hide();
  }
 });
});
</script>


Save Page.














Download jquery-1.3.2.js from link

Friday, June 9, 2017

Start Workflow using Powershell - SharePoint 2013


# URL of Site
$web = Get-SPWeb -Identity "https://weburl/url"

$manager = $web.Site.WorkFlowManager

# Name of the list
$list = $web.Lists["ListName"]

# Name of the Workflow
$assoc = $list.WorkflowAssociations.GetAssociationByName("WorkflowName","en-US")

$data = $assoc.AssociationData
$items = $list.Items
foreach($item in $items)
 {
 $wf = $manager.StartWorkFlow($item,$assoc,$data,$true)
 }

$manager.Dispose()
$web.Dispose()
#

Friday, June 2, 2017

Add List Filter Search - SharePoint 2013

How to add custom filter search in SharePoint list?



Add Content editor web part













Instert code:

<script type="text/javascript">
 function RedirectUrl() {
 var tb = document.getElementById("tbSearch").value;
 var cs = document.getElementById("sfield").value;
 var url = "";

 if (tb != "") {
  if (cs == "Title" || cs == "Country"){
  url = "FilterField1=" + cs + "&FilterValue1=" + tb;
  window.location.href = "AllItems.aspx?" + url;
  }
  else { 
  url = "FilterName=" + cs + "&FilterMultiValue=*" + tb + "*";
  window.location.href = "AllItems.aspx?" + url;
  }
  }
  else {
  return false;
  }
 }
 function ClearUrl() {
 window.location.href = "AllItems.aspx";
 }
</script>
Search Field: <select id="sfield">
<option selected value="Title">Title</option>
<option value="Country">Country</option>
</select>
&nbsp;
Search text: <input type="text" id="tbSearch" />
<input type="button" id="btnSearch" value="Search" onclick="return RedirectUrl();" />
<input type="button" id="btnClear" value="Clear" onclick="return ClearUrl();" />















Save page.














Insert value in search box

Thursday, May 25, 2017

Hide Top Bar, Ribbon, and Quick Launch - SharePoint 2013

How to Hide Top Bar, Ribbon, and Quick Launch in SharePoint 2013 with css?

Edit page and add Content Editor Web Part














Insert code

<style type="text/css">
    #s4-ribbonrow, .ms-cui-topBar2, .s4-notdlg, .s4-pr s4-ribbonrowhidetitle, .s4-notdlg noindex, #ms-cui-ribbonTopBars, #s4-titlerow, #s4-pr s4-notdlg s4-titlerowhidetitle, #s4-leftpanel-content {display:none !important;}
    .s4-ca {margin-left:0px !important; margin-right:0px !important;}
    .ms-core-navigation { display: none; }
    #contentBox { margin-left: 0; }
</style>



Wednesday, May 24, 2017

Unable to delete site collection - SharePoint 2013

How to delete corrupted SharePoint Site Collection?

Error: Remove-SPSite : <nativehr>0x80070003</nativehr><nativestack></nativestack>


 




Solution:

$site = Get-SPSite https://siteurl

$siteId = $site.Id

$siteDatabase = $site.ContentDatabase

$siteDatabase.ForceDeleteSite($siteId, $false, $false)

Tuesday, January 24, 2017

Problem with database collation change SQL Server



The Database could not be exclusively locked to perform the operation.
ALTER DATABASE failed. The default collation of database 'DatabaseName' cannot be set to "Collaction Name".  (Microsoft SQL Server, Error: 5030)




use master
go
ALTER DATABASE xxx SET SINGLE_USER WITH ROLLBACK IMMEDIATE
go
ALTER DATABASE xxx COLLATE yyyy
go
ALTER DATABASE xxx SET MULTI_USER
go




WARNING: this call to SET SINGLE_USER WITH ROLLBACK IMMEDIATE will disconnect anyone who might be connected to that database without warning, without chance of saving data! 

Monday, January 9, 2017

Delete all subsites (All Child Webs) - SharePoint 2013 and PowerShell

Error: There was a problem deleting Web site "/Subsite". Sites that have subsites or certain apps can't be deleted. Please try again after deleting all subsites and removing the apps.


Solution:
Run PowerShell command
 
 
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
Function Remove-ChildSites([Microsoft.SharePoint.SPWeb]$Web)
{
 Foreach($ChildWeb in $Web.Webs)
  {
  #Call the function recursively TO DELETE all sub-childs
  Remove-ChildSites($ChildWeb)
 }
    Write-host Removing web $Web.Url
  
    #Remove the web
 Remove-SPWeb $Web -Confirm:$false
}
 
$ParentWebURL="http://portal/site/"
 
#Get the Parent Web
$ParentWeb= Get-SPWeb $ParentWebURL
 
#Call the function to remove all child webs
Remove-ChildSites $ParentWeb