Wednesday 23 December 2015

Disable LoopBackCheck


DisableLoopBack check to avoid multiple prompts while accessing sites



New-ItemProperty HKLM:\System\CurrentControlSet\Control\Lsa -Name "DisableLoopbackCheck" -value "1" -PropertyType dword

Create App Management Service Applications using Powershell



cls
asnp "*sh*"


Get-SPServiceInstance |?{$_.TypeName -like  "App Management Service"} | Start-SPServiceInstance

Get-SPServiceInstance | ? {$_.TypeName -like "Microsoft SharePoint Foundation Subscription Settings Service"} | Start-SPServiceInstance

#Provide the service account as per your configuration 

$account=Get-SPManagedAccount -Identity CORP\Svc.AppsAccount

#configure Subscription Service Application for Sharepoint

$appPoolSubsvc=New-SPServiceApplicationPool -Name "SubscriptionServericeAppPool" -Account $account
$appSubSvc=New-SPSubscriptionSettingsServiceApplication -ApplicationPool $appPoolSubsvc -Name "Subscription Service Application" -DatabaseName SubscriptonServiceDB
$proxySubSvc=New-SPSubscriptionSettingsServiceApplicationProxy -ServiceApplication $appSubSvc
Write-Host "Subscirption Service Application Created"


$appPoolAppSvc= New-SPServiceApplicationPool -Name "AppManagementServiceAppPook" -Account $account
$appsMgmtSvc= New-SPAppManagementServiceApplication -ApplicationPool $appPoolAppSvc -Name "Apps Management Service" -DatabaseName "AppsDb"
$proxyAppSvc=New-SPAppManagementServiceApplicationProxy -ServiceApplication $appsMgmtSvc

Write-Host "Apps Management Service Application Created"

Monday 30 November 2015

Microsoft SharePoint is not supported in 32-bit process. Please verify that you are running in a 64-bit executable.






Solution :


In visual Studio Go to Project Properties ->Build  -> Platform target -Any CPU  and 

Click on Build which will fix the issue.












Copy files and Folders in SharePoint from One Site to anonther Site

Below PowerShell Script to copy files and folders from one Site to anonther through powershell

cls

asnp "*sh*"

write-Host "Please Provide the Source web site url and Desstination url"-foregroundcolor white -backgroundcolor blue
##
$SourceWebURL = read-host "Enter Source WebURL:"
$DestinationWebURL = read-host "Enter Destination WebURL:"
##
write-Host "Please Provide the Source library title and Destination library title"-foregroundcolor white -backgroundcolor blue
##
$SourceLibraryTitle = read-host "Enter Source LibraryTitle:"
$DestinationLibraryTitle = read-host "Enter Destination LibraryTitle:"
##
write-output "Started...";
##
$sWeb = Get-SPWeb $SourceWebURL
$sList = $sWeb.Lists | ? {$_.Title -eq $SourceLibraryTitle}
$dWeb = Get-SPWeb $DestinationWebURL
$dList = $dWeb.Lists | ? {$_.title -like $DestinationLibraryTitle}

$AllFolders = $sList.Folders
$RootFolder = $sList.RootFolder
$RootItems = $RootFolder.files

foreach($RootItem in $RootItems)
{
    $sBytes = $RootItem.OpenBinary()
    $dFile = $dList.RootFolder.Files.Add($RootItem.Name, $sBytes, $true)

    $AllFields = $RootItem.Item.Fields | ? {!($_.sealed)}

    foreach($Field in $AllFields)
    {
        if($RootItem.Properties[$Field.Title])
        {
            if(!($dFile.Properties[$Field.title]))
            {
                $dFile.AddProperty($Field.Title, $RootItem.Properties[$Field.Title])
            }
            else
            {
                $dFile.Properties[$Field.Title] = $RootItem.Properties[$Field.Title]
            }
        }
    }
    $dFile.Update()
}

foreach($Folder in $AllFolders)
{
    #Remove-Variable ParentFolderURL
        $varExists = Get-Variable ParentFolderURL -ErrorAction SilentlyContinue

    if($varExists -ne $null){

           Remove-Variable ParentFolderURL  }
    $i = 0
    
    $FolderURL = $Folder.url.Split("/")
        
    while($i -lt ($FolderURL.count-1))
    {
    $ParentFolderURL = "$ParentFolderURL/" + $FolderURL[$i]
    $i++
    }
    
    $CurrentFolder = $dList.Folders | ? {$_.url -eq $ParentFolderURL.substring(1)}
    if(!($CurrentFolder.Folders | ? {$_.name -eq $Folder.Name}))
    {
        $NewFolder = $dlist.Folders.Add(("$DestinationWebURL" + $ParentFolderURL), [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, $Folder.name)
        $NewFolder.update()
    }
    else
    {
        $NewFolder = $dList.Folders | ? {$_.name -eq $Folder.Name}
    }
    $AllFiles = $sList.Items
    $sItems = $Folder.folder.Files
    
    if($Folder.Folder.Files.count -gt 0)
    {
        foreach($item in $sItems)
        {
            
            #$Relative = ($Item.ServerRelativeUrl).substring(1)
            $Relative = ($Item.Url)
            $TargetItem = $AllFiles | ? {$_.URL -eq $Relative}
            $sBytes = $TargetItem.File.OpenBinary()
            $dFile = $Newfolder.Folder.Files.Add($TargetItem.Name, $sBytes, $true)
            $AllFields = $TargetItem.Fields | ? {!($_.sealed)}
            
            foreach($Field in $AllFields)
            {
                if($TargetItem.Properties[$Field.Title])
                {
                    if(!($dFile.Properties[$Field.title]))
                    {
                        $dFile.AddProperty($Field.Title, $TargetItem.Properties[$Field.Title])
                    }
                    else
                    {
                        $dFile.Properties[$Field.Title] = $TargetItem.Properties[$Field.Title]
                    }
                }
            }
            $dFile.Update()
        }
    }
}
Write-Host "All Files are moved successfully" -foregroundcolor green

Export User alerts/ Export User Subscribed alerts in PowerShell


Write User Subscribed Alerts to CSV

Below is the PowerShell script which will export the Users who have subscribed alerts to lists and librarys in the site.

SharePoint Out of the box the provides to view users alerts SiteSettings->Usrs alerts , select the user from dropdownlist which will show huge chunk of users , select the user and click on update button which will show documentlibrary or lists which selected user has subscribed alerts.

But above requirement will not solve our problem where we need to view the consolidated list users instead of selecting each time single user through UI,

Either we can go through custom coding or powershell , below is the powershell script


cls

asnp "*sh*"

$web=Get-SPWeb http://SharePoint2013:1555/

$alerts=$web.Alerts

foreach($alert in $alerts)

{

$alert.List.Title + " ......" $alert.User.DisplayName + "   " + 

$alert.User.DisplayName  | Out-File E:\Ms\UserAlerts.csv -Encoding 

ascii -Append -Width 100

}





Friday 11 September 2015

Client Object Model Code to Upload a File /CSOM File Upload

Below is the Client object Model code to upload file using CSOM. Here i am using Console Application and hard coding the path of file ofcourse you can create an windows form with user input to read the path, in the below case i am using console model .

Since i am uploading file to different domain so i am passing my credentials ("Username","PassWord","DomainName"); and I am uploading to Document Library Named Documents.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using System.IO;
using System.Data;
using System.Net;

namespace CSOMFileUpload
{
    class Program
    {
        static void Main(string[] args)
        {
            string url = "http://SP2013.dev.com/";

            ClientContext ctx = new ClientContext(url);
            try
            {
                ctx.Credentials = new NetworkCredential("fasx669", "Wintertime123", "world");
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadKey();
             
           
            }
            try
            {
                FileCreationInformation file1 = new FileCreationInformation();
                file1.Url = "SoreddyManjunath.txt";
                file1.Content = System.IO.File.ReadAllBytes(@"C:\MSFile-Manju.txt");
                List doclib = ctx.Web.Lists.GetByTitle("Documents");
                Microsoft.SharePoint.Client.File myfile = doclib.RootFolder.Files.Add(file1);
                ctx.Load(myfile);
                ctx.ExecuteQuery();
                ListItem item = myfile.ListItemAllFields;
                item["Title"] = "Welcometo File upload";
                item.Update();
                ctx.Load(item);
                ctx.ExecuteQuery();
                Console.WriteLine("File uploaded Succesfully");
                Console.ReadKey();
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadKey();
            }

        }
    }
}



Output:






Thursday 23 April 2015

Update-SPDistributedCacheSize : Cannot bind parameter 'CacheSizeInMB'. Cannot convert value "250 MB" to type "System.UInt32". Error: "Input string was not in a correct format."



Get Distributed Cache :

To check the existing memory allocation for the Distributed Cache service on a server, run the following command at the SharePoint Management Shell command prompt:


cls
asnp "*sh*"
Use-CacheCluster

Get-AFCacheHostConfiguration -ComputerName "spdev" -CachePort "22233" 



Update Cache :


  1. Stop the Distributed Cache service on all cache hosts. To stop the Distributed Cache service, go to Services on Server in Central Administration, and Stop the Distributed Cache service on all cache hosts in the farm.
  2. To reconfigure the cache size of the Distributed Cache service, run the following command one time only on any cache host at the SharePoint Management Shell command prompt:
    Update-SPDistributedCacheSize -CacheSizeInMB CacheSize 
    
    
    Where:
    • CacheSize is the cache size's memory allocation assignment in MB. In the previous example, the cache size was calculated at 7168 MB for a server with 16 GB of total memory.
  3. Restart the Distributed Cache service on all cache hosts. To restart the Distributed Cache service, go to Services on Server in Central Administration, and Start the Distributed Cache service on all cache hosts in the farm.
ex:

Update-SPDistributedCacheSize -CacheSizeInMB "250 MB" 

Update-SPDistributedCacheSize : Cannot bind parameter 'CacheSizeInMB'. Cannot 
convert value "250 MB" to type "System.UInt32". Error: "Input string was not in 
a correct format."
At line:1 char:46
+ Update-SPDistributedCacheSize -CacheSizeInMB "250 MB"
+                                              ~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Update-SPDistributedCacheSiz 
   e], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.SharePoin 
   t.PowerShell.SPCmdletConfigureDistributedCacheSize


In order to Fix the issue remove "MB" in string , pass only int value

Update-SPDistributedCacheSize -CacheSizeInMB  250

Again We will See updated Cache in the Server.

cls
asnp "*sh*"
Use-CacheCluster

Get-AFCacheHostConfiguration -ComputerName "spdev" -CachePort "22233" 

You can see Value increased to 250 MB.