Thursday, September 17, 2015

How to Enable or Disable Document Property Promotion (ParserEnabled) in SharePoint 2013

How to Enable or Disable Document Property Promotion (ParserEnabled) in SharePoint 2013

This is the way to solve issues on uploading document into SharePoint 2013 Document Library. After upload the document into SharePoint library, the document had the metadata (document properties) of the original document. Metadata that is changed on the document will revert to the original values that were given to the document; this occurs both from SharePoint and from MS Word.
 
Solution 1 – Remove Document Properties & personal Information using Inspect
1.      Open file using Microsoft office
2.      Click file and the inspect document (check for issues)
3.      Tick only on document properties & personal information
4.      Inspect & then remove
5.      Then Save
6.      Upload document into SharePoint.
 
Solution 2 – Disable Document Property Promotion using PowerShell
The SPWeb object has a ParserEnabled property, which enables or disables this functionality.  Using the PowerShell script, we can turn off this functionality at the web level. Sample script to disable the document parser feature in SharePoint:
1.      Open the SharePoint 2013 Management Shell as Administrator.
2.      Run the following script to disable document property promotion.
 
C:\> $web
        Url
        ---
        http://spfe01/sites/SITENAME/SUBSITENAME
C:\> $web.parserenabled
        True
C:\> $web.parserenabled  = $false
C:\> $web.Update()
C:\> $web.parserenabled
        false
 
 
3.      Document Property Promotion is disabled successfully and when you try to upload a document you will find that all the fields are empty
 
Reference 3:http://fairulshahrizat.blogspot.in/2014/10/how-to-enable-or-disable-document.html

Friday, September 4, 2015

Thursday, September 3, 2015

Get all Columns of List (including Hidden) using powershell

Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Out-Null

$url = "SiteUrl/"
$listName = "Listname"
$path ="C:\Temp\Columns.csv"

$web = Get-SPWeb $url
$list = $web.Lists.TryGetList($listName)
$list.ContentTypes | % { $_.FieldLinks } | select Name |  Export-Csv -path $path

Get all fields inside ContentType sharepoint powershell


cls
set-variable -option constant -name url -value "http://siteurl"  # Site collection
#set-variable -option constant -name out -value "ContentTypes.csv"  # Site collection
# End of settings

$site = new-object Microsoft.SharePoint.SPSite($url)
$cts = $site.rootweb.ContentTypes["ContentTypeName"]


ForEach ($id in $cts)
{
  ForEach ($field in $id.Fields)
  {
  Write-Host $field.Title
  Write-Host $field.InternalName
 
  }
}

$site.Dispose()

echo "Finished!"

UnHide hidden column using powershell 2013 sharepoint

# First load SharePoint Core Assembly

###################################################################################
#                                                                                 #
###################################################################################

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
cls
$web = Get-SPWeb  "http://webapp/siteurl"
$listLegComp = $web.Lists["ListtemplateHidden"]
#$ContentType=$web.ContentTypes[""]
#$myfield=$ContentType.Fields[""]


$pcField = $listLegComp.Fields.GetFieldByInternalName("Hidden2Column")

if(!$pcField.CanToggleHidden)
{
   $bindingFlags = [Reflection.BindingFlags] "NonPublic,Instance"
   [System.Type] $type = $pcField.GetType()
   [Reflection.MethodInfo] $mdInfo = $type.GetMethod("SetFieldBoolValue",$bindingFlags)
   $object = [System.Object] @("CanToggleHidden",$true)
   $mdInfo.Invoke($pcField,$object)
   $pcField.Hidden = $true
   $field.ShowInEditForm = 1;



# Controls Field in New Form
$pcField.ShowInNewForm = 1;

# Controls Field in New Form
$pcField.ShowInDisplayForm = 1;

# Hides fields from list settings
$pcField.ShowInListSettings = 1;

# Hides fields from version history
$pcField.ShowInVersionHistory = 1;

# Hides fields form selection in views
$pcField.ShowInViewForms = 1;
#check allow deletion
   $pcField.AllowDeletion = $true
   $pcField.Update()
}



$listLegComp.Update()
$web.Dispose()

Get login Name using csom

  public static string GetUserLoginName(string Context, int UserID)
        {
         
       
            string LoginName = string.Empty;
string EmailId = string.Empty;
            try
            {
                using (ClientContext ctx = new context(Context))
                {

//credentials
                    if (ctx != null)
                    {


                        UserCollection users = ctx.Web.SiteUsers;
                        ctx.Load(users);
                        ctx.ExecuteQuery();
                        User RequestorUser = users.GetById(UserID);
                        ctx.Load(RequestorUser);
                        ctx.ExecuteQuery();
                        LoginName = RequestorUser.LoginName;
    EmailId= RequestorUser.EmailID;
                    }
                }
            }
            catch (Exception ex)
            {

         
            }


            return LoginName;
        }

Get DocLibrary Size using Csom SharePoint

 public static string getDocLibraySize(string Context, string DocLibraryName)
        {
            double docLibSize = 0;
            try
            {
                using (ClientContext ctx = new ClientContext(Context))
            {
               


               // ctx.Credentials = new NetworkCredential("userid", "paswword", "domain");
                List oList = ctx.Web.Lists.GetByTitle(DocLibraryName);
                CamlQuery oQuery = new CamlQuery();
//get all files exclude the folders and bring all items
                oQuery.ViewXml = "<View Scope='RecursiveAll'>"
   + "<Query>"
   + "   <Where>"
   + "      <Eq><FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value></Eq>"
   + "   </Where>"
   + "</Query>"
   + "</View>";


                ListItemCollection collListItem = oList.GetItems(oQuery);
                ctx.Load(oList);
                ctx.ExecuteQuery();
                ctx.Load(collListItem);
                ctx.ExecuteQuery();
                double ListSize = 0;


                foreach (ListItem oListItem in collListItem)
                {
                    double TotalFileSize = 0;

                    Microsoft.SharePoint.Client.File file = oListItem.File;
                    ctx.Load(file);
                    ctx.ExecuteQuery();
                    FileVersionCollection versions = file.Versions;

                    ctx.Load(versions);
                    ctx.ExecuteQuery();
                    TotalFileSize = TotalFileSize + file.Length;
                    int fileCount = versions.Count;
                    if (fileCount >= 1)
                    {
                        if (versions != null)
                        {
                            double ItemVersionSize = 0;
                            foreach (FileVersion _version in versions)
                            {
                                ctx.Load(_version);
                                ctx.ExecuteQuery();
                                ItemVersionSize = ItemVersionSize + _version.Size;

                            }
                            TotalFileSize = TotalFileSize + ItemVersionSize;
                        }
                    }
                    ListSize = ListSize + TotalFileSize;


                }

                //Converting byte to MB and rounding to 2 decimal digits
                docLibSize = Math.Round((ListSize / (1024 * 1024)), 2);
               
                //webSize = DefineSizeWithUnitMeasure(ListSize, out unitMeasure);
                Console.WriteLine("Total DocLibrary Size : " + docLibSize.ToString());
            }
            }
            catch (Exception e)
            {

                Console.WriteLine("Unable to Get DocLibrarySize");
            }

            return docLibSize.ToString();
        }

        public static double DefineSizeWithUnitMeasure(double sizeInBytes, out string unitMeasure)
        {
            unitMeasure = " Bytes";
            double size = sizeInBytes;

            if (size > 1024)
            {
                size = sizeInBytes / 1024d;//KB
                unitMeasure = " KB";
            }
            if (size > 1024)
            {
                size = size / 1024d;//MB
                unitMeasure = " MB";
            }
            if (size > 1024)
            {
                size = size / 1024d; //GB
                unitMeasure = " GB";
            }

            if (size > 1024)
            {
                size = size / 1024d; //TB
                unitMeasure = " TB";
            }

            return size;
        }