Thursday, September 3, 2015

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;
        }

Friday, August 28, 2015

Login with different user Sharepoint 2013

Change the siteurl with yours and it will ask you for the new username and password


YourSite/_layouts/closeConnection.aspx?loginasanotheruser=true

Monday, September 22, 2014

The length of the URL for this request exceeds the configured maxUrlLength value SharePoint 2013 Workflow manager httprequest

Error:The length of the URL for this request exceeds the configured maxUrlLength value in SharePoint 2013 Using Workflow Manger


it can be solved by updating simple change in web.config as below:


<configuration>
  <system.web>
<httpRuntime maxRequestLength="51200" requestValidationMode="2.0" maxUrlLength="2000" /> 
  </system.web>
</configuration>
   

get the url from the browser using javascipt var fullUrl = window.location.pathname + window.location.search;

var fullUrl = window.location.pathname + window.location.search;