none
Display easily each line, item or document in powershell RRS feed

  • Question

  • Hi all,

    Can you give me the good syntax to list/display with powershell the documents for a libray and the items for a list  please ?

    In a second time I would like to loop on each column to check if the field is a metadata or not.

    Thanks in advance.

    Regards,

    EM.

    Monday, December 19, 2016 11:39 AM

Answers

  • Hi,

    This is unpredictable behavior especially in SharePoint 2013, but we could get the StaticName like below:

      var PS4 = list.Fields.GetByInternalNameOrTitle("PS4");

                    context.Load(PS4);

                    context.ExecuteQuery();

                    Console.WriteLine(PS4.StaticName);

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    • Marked as answer by Elmata Tuesday, December 27, 2016 3:15 PM
    Monday, December 26, 2016 5:55 AM

All replies

  • Hi,

    Just sample code for your reference, you need update based on your own scenario:

    Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"  
    Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  
      
    $siteURL = "http://sp:12001"  
    #$userId = ""  
    #$pwd = Read-Host -Prompt "Enter password" -AsSecureString  
    #$creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)  
    $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)  
    #$ctx.credentials = $creds  
    try{  
        $lists = $ctx.web.Lists  
        $list = $lists.GetByTitle("Doc2")  
        $listItems = $list.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())  
        $ctx.load($listItems)  
        
    	$fields =$list.Fields	
    	$ctx.load($fields) 
    	
        $ctx.executeQuery()  
        foreach($listItem in $listItems)  
        {  
            Write-Host "ID - " $listItem["ID"] "Title - " $listItem["Title"]
    		foreach($field in $fields){
    			if(!$field.Sealed){
    				write-host $listItem[$field.StaticName]
    			}
    		}
        }   
    }  
    catch{  
        write-host "$($_.Exception.Message)" -foregroundcolor red  
    } 
    

    One link for your reference:

    http://www.c-sharpcorner.com/article/list-item-operations-using-csom-with-powershell-for-sharepoi/

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Tuesday, December 20, 2016 5:33 AM
  • Hi Lee,

    It's a very good base to learn and work. Many thanks !

    Howerver I can't display the name of the document. I try with "Filename" or "LinkFilename" in vain...

    Do you know why ?

    Regards, 

    EM

    Wednesday, December 21, 2016 9:04 AM
  • Hi,

    It seems LinkFilename/Filename are not available in CSOM yet, If you want to get file name/URL. You could try to use below fields:

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Wednesday, December 21, 2016 11:03 AM
  • Just wonderful !

    Do you have the all CSOM references ?

    Thanks a lot Lee :)

    Regards,

    EM.

    Wednesday, December 21, 2016 2:15 PM
  • Hi,

    As CSOM assembly may update in future, you could try to check ALL available fields in a console application. Below is sample code for your reference:

    Create a console application and reference below assembly, you could find them in C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI

     static void Main(string[] args)
            {            
                using (var context = new ClientContext("http://sp:12001"))
                {                                               
                    context.Credentials = new NetworkCredential(@"contoso\administrator", "PW");
    
                    #region MyRegion
                    Web web = context.Web;
                    context.Load(web);
                    context.ExecuteQuery();
                    List list = context.Web.Lists.GetByTitle("Doc2");
                    context.Load(list);
                    context.ExecuteQuery();
    
                    var item = list.GetItemById(1);
                    context.Load(item);
                    context.ExecuteQuery();
                  
                    #endregion
                    Console.ReadKey();
                }
    
            }

    One thread for your reference:

    https://dev.office.com/blogs/using-correct-csom-version-for-sharepoint-customizations

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Thursday, December 22, 2016 9:08 AM
  • Hi Lee,

    You rock.

    Can I check/test the type of a column ? (metadata for example)

    Best regards,

    EM

    Thursday, December 22, 2016 9:47 AM
  • Hi,

    Yes, you could check the property FieldTypeKind/TypeAsString, for example:

    foreach(var _Item in item.FieldValues){
                        var _Field = list.Fields.GetByInternalNameOrTitle(_Item.Key);
                        context.Load(_Field);
                        context.ExecuteQuery();
                        Console.WriteLine(_Field.FieldTypeKind+"--"+_Field.TypeAsString);
                    }

    More details here:

    https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.field_properties.aspx

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Thursday, December 22, 2016 10:14 AM
  • Hi Lee,

    Many thanks.

    To close this issue, can you tell explain me the following behavior.

    I create a new metadata column called "PS4" and to target the label I have to make this call $item[" _x0050_S4"].TermGuid instead $item["PS4"].TermGuid. do you know why this change ?

    My first column was named "MMD" (for Managed Meta Data ^^) and this name didn't change.

    Can you tell me too how I can export the write-host result in a file ?

    Regards,

    EM.


    • Edited by Elmata Friday, December 23, 2016 3:10 PM
    Friday, December 23, 2016 10:14 AM
  • Hi,

    This is unpredictable behavior especially in SharePoint 2013, but we could get the StaticName like below:

      var PS4 = list.Fields.GetByInternalNameOrTitle("PS4");

                    context.Load(PS4);

                    context.ExecuteQuery();

                    Console.WriteLine(PS4.StaticName);

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    • Marked as answer by Elmata Tuesday, December 27, 2016 3:15 PM
    Monday, December 26, 2016 5:55 AM