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.



Thursday, 16 October 2014

The content type “Document Collection Folder” at “…” is sealed.



 The content type “Document Collection Folder” at “…” is sealed - SharePoint 2013


In an Asset Library When user tries to upload a Video file below error pops up 

but this won't pop up if a user tries to upload image files


Solution to Above issue is We need to Activate "Video and Rich Media"  Feature at SiteCollection Level and he has to delete the existing Asset library and create the new Asset library.



Wednesday, 15 October 2014

Break List Item Permissions PowerShell


Break Permissions Item Level / Custom Permissions at item level


Blow PowerShell script will retain list permission and give unique permissions atitem level.

We had a requirement to break all item level permissions and remove all the Groups and individual permissions at the item level and Add Owners group and Approvers group  to all items and also it should retain the permissions of item created by.

At the  item level it will delete the all inherited Groups from List and Add specific groups to each item name called " Owners" with  "Full Control " and "Approvers" with "Design" Permissions.



 clear

asnp "*sh*"

$web=Get-SPWeb -Identity "http://weshare.sedwdev.local/sites/teste"

Write-Host " Author is " $web.Author.DisplayName

$list=$web.Lists.TryGetList("SourceList")

foreach($item in $list.Items)
{
    if(!$item.HasUniqueRoleAssignments)
 {
        $item.BreakRoleInheritance($false)

 for($i=0 ; $i -lt $item.RoleAssignments.Count;$i++)
    {

   if(!$item["Created By"])
{
     $item.RoleAssignments.remove($i)

    $item.Title + "permissions Removed "
  
  } }

  foreach( $grp in $web.Groups)
  {
    
     if ($grp.Name -match "teste Owners")
     {

     $rolAsgnmt=New-Object Microsoft.SharePoint.SPRoleAssignment($grp);

     $roldef=$web.RoleDefinitions["Full Control"]

     $rolAsgnmt.RoleDefinitionBindings.Add($roldef);

     $item.RoleAssignments.Add($rolAsgnmt);

     " Item title is  "+ $item.Title + "  Group Added is  " + $grp.Name

     $item.Update();
    
     }

    elseif ($grp.Name -match "Approvers")
     {

     $rolAsgnmt1=New-Object Microsoft.SharePoint.SPRoleAssignment($grp);

     $roldef1=$web.RoleDefinitions["Design"]

     $rolAsgnmt1.RoleDefinitionBindings.Add($roldef1);

     $item.RoleAssignments.Add($rolAsgnmt1);

     " Item title is  "+ $item.Title + "  Group Added is  " + $grp.Name

     $item.Update();
     }
     }  
 }
 }

OutPut:





Wednesday, 8 October 2014

Remove Duplicates in Search Results Webpart


Remove duplicates in Search Results Webpart


asnp "*sh*"


$resultsPage = "Pages/default.aspx"

$resultsUrl = "$siteUrl/$resultsPage"

$spweb = Get-SPWeb $siteUrl

$page = $spweb.GetFile($resultsPage)

# Check if the page is checked out to someone if so override the checkout

$bool=$page.CheckedOut

if($bool)
{
$page.UndoCheckOut();
}

# checkout the Page

$page.CheckOut()

# Get the web part manager for the page
$webPartManager = $spweb.GetLimitedWebPartManager($resultsUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

# And pull the web part we want to work on
$webpart = $webpartmanager.WebParts | ? { $_.Title -eq 'Latest News' }

$dataProvider = ConvertFrom-Json $webpart.DataProviderJSON

$dataProvider.TrimDuplicates = 'True'

$webpart.DataProviderJSON = ConvertTo-Json $dataProvider -Compress

$webpartmanager.SaveChanges($webpart)

$page.CheckIn('Setting the duplicates in LatestNews');

$page.Publish('Duplicates were set in Latest News Webpart');

$spweb.Dispose();


OutPut:


Tuesday, 7 October 2014

Increase Site Quota using PowerShell

PowerShell Script to Increase Storage Individual Quota


Increasing the SiteCollection Storage without Specifying Site Quota that is individual level,

Here we are increasing the site quota to 5 GB, and setting Warning to 4.5 GB



clear

asnp "*sh*"

$site=Get-SPSite -Identity "http://sedwdevrtm:432/sites/ProductsSite"

# Here We are setting Max storage to 5GB and Warning level to 4GB

Set-SPSite -Identity $site -MaxSize 5GB -WarningSize 4.75GB 

# we are seeing what is the maximum storage and warning level set for the site.

$site.Quota.StorageMaximumLevel/1GB


$site.Quota.StorageWarningLevel/1GB  


Output:




Monday, 4 August 2014

Rename a Site using Powershell/ Rename SiteCollection Powershell


PowerShell Script to rename a SiteCollection

Here are renaming the site from energimiljo to Energymill

cls

asnp "*sh*"

$site=Get-SPSite -Identity "http://SP2013dev.com/sites/energimiljo/"

$site.Rename("http://SP2013dev.com/sites/energymills")

$site.Dispose()