2

in a web application there is a document library and daily 1000 to 2000 documents are uploaded by users,

so here i want to calculate the average size of documents uploaded to this web application, so here i want to create a view based filesize column and apply filter on this column.

ex: filesize >500 KB

  • What condition you would want to have in your filter? Like you would want to show documents whose file size is greater than 500KB? – Asad Refai Aug 10 '15 at 7:44
  • i did not fount filesize column in filter condition while i creating a view..i taken the 500 kb is for example. i will check in some range values like 500 - 1000 kb and 1000 - 2000 – adilahmed Aug 10 '15 at 8:11
  • 1
    There is a column with internal name "FileSizeDisplay" try to create a view using that column programatically, or export any listview webpart and update the query – sekhar Aug 10 '15 at 9:23
2

You can create a view with File Size placed in the query using CSOM PowerShell. I have used the query mentioned by @Yayati. You can replace with yours.

this function need not compulsorily run on the SharePoint server. You can also run it from your remote machine provided you have Client Object Model namespaces on your remote machine.

function Create-ListView()
{
    param(
        [Parameter(Mandatory=$true)][string]$url,
        [Parameter(Mandatory=$false)][System.Net.NetworkCredential]$credentials,
        [Parameter(Mandatory=$true)][string]$listName,
        [Parameter(Mandatory=$true)][string]$viewName
    )

    begin{
        try
        {
            #get Client Object
            $context = New-Object Microsoft.SharePoint.Client.ClientContext($url)
            $context.Credentials = $credentials

            #Retrieve List
            $list = $context.Web.Lists.GetByTitle($listName)
            $context.Load($list)
            $context.ExecuteQuery()


        }
        catch
        {
            Write-Host "Error while getting context. Error -->> "  + $_.Exception.Message -ForegroundColor Red
        }
    }
    process{
        try
        {
            $viewQuery = '<Where><Lt><FieldRef Name='File_x0020_Size' /><Value Type='Number'>10000</Value></Lt></Where>' 
            $viewFields = New-Object System.Collections.Specialized.StringCollection  
            $viewFields.Add("Title")
            $viewFields.Add("File_x0020_Size")
            $ViewInfo = New-Object Microsoft.SharePoint.Client.ViewCreationInformation
            $ViewInfo.Query = $viewQuery
            $ViewInfo.RowLimit = 50
            $ViewInfo.ViewFields = $viewFields 
            $ViewInfo.Title = $viewName
            $ViewInfo.SetAsDefaultView = $false



            $view = $list.Views.Add($ViewInfo)
            $context.Load($list.Views)
            $context.ExecuteQuery()
            Write-Host "View added successfully" -ForegroundColor Green

        }
        catch
        {
            Write-Host ("Error while creating view for a List. Error -->> " + $_.Exception.Message) -ForegroundColor Red
        }
    }
    end{
        $context.Dispose()
    }


}

Usage:

$credentials = Get-Credential
Create-ListView 'http://yoursite' $credentials 'Your Library Name' 'Name of your view'
1

You are correct. The FileSize column is by default is not visible in filter section of View. So you can create a custom view using SharePoint Designer by using CAML Query.
Here are steps to create custom view:

  1. Go to Document Library settings -> Create View -> Custom View in SharePoint Designer
  2. Open the custom view in SharePoint Designer.
  3. Search for <View> tag and replace <Query/> tag with your CAML Query. e.g. here is the query to show file having filesize < 10kb

    <Query><Where><Lt><FieldRef Name='File_x0020_Size' /><Value Type='Number'>10000</Value></Lt></Query></Where>
    


    (Note : I have taken 1kb = 1000 bytes)

  4. Save your view and if required make it default.
  • i will try this and let you back – adilahmed Aug 11 '15 at 5:57
1

Provided you have a View with the FileSize column displayed all the information is available in de ctx object.

You can process this with CSR or in a CEWP

Proof of concept code in F12 Dev Tools:

function countFileSize(min,max){//in bytes
    var total=0,
        count=0;
    ctx.ListData.Row.forEach(function(row){
        var size=parseInt(row.File_x0020_Size);
        console.log('item ID',size,'item ID:',row.ID,row.FileLeafRef);
        if(size>min && size<max){
            count++;
            total+=size;
        } else {
            console.error('outside boundaries:',size,row.FileLeafRef);

        }
    });
    console.info('With boundaries:',min,max,' there are',count,'files','totalling',total,'bytes');
    return(total/count);
};
console.clear();
var average=countFileSize(0,100000);//bytes
console.info( 'average:',average,'bytes' );

iCSR

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.