PS SharePoSH:\> SPListItem / SPFile SharePoint + PowerShell User Group March 27, 2013 http://www.SharePoSH.com @SharePoSH.

Download Report

Transcript PS SharePoSH:\> SPListItem / SPFile SharePoint + PowerShell User Group March 27, 2013 http://www.SharePoSH.com @SharePoSH.

PS SharePoSH:\>
SPListItem / SPFile
SharePoint + PowerShell User Group
March 27, 2013
http://www.SharePoSH.com
@SharePoSH
PS SharePoSH:\>
Agenda
• Hello!
• Group Logistics (GoToMeeting/ website /
register / email / twitter)
• SPListItem / SPFile
• Next meeting
• Survey!
http://www.SharePoSH.com
@SharePoSH
PS SharePoSH:\>
Hello!
Raymond Mitchell
• SharePoint Consultant
– http://www.Rackspace.com
• Author
• Dad
• Information Worker Kid
– http://www.iwkid.com
• SharePoSHing since 2008
http://www.SharePoSH.com
@SharePoSH
PS SharePoSH:\>
What about you?
http://www.SharePoSH.com
@SharePoSH
PS SharePoSH:\>
SPListItem
• List item
• Library item
• SPListItemCollection
– SPList.Items
http://www.SharePoSH.com
@SharePoSH
PS SharePoSH:\>
SPListItem
SPList.Items[5]
Iterating by index can be VERY expensive
http://www.SharePoSH.com
@SharePoSH
PS SharePoSH:\>
SPListItem
• More ways to get items using SPList members
– GetItems
•
•
•
•
String[]
SPQuery
SPView
SPQuery, String
items only with specific fields
items by query
items by view
by query and view
– GetItemsWithUniquePermissions
http://www.SharePoSH.com
@SharePoSH
PS SharePoSH:\>
SPListItem
• More ways to get items using SPList members
– GetItemById & GetItemByIdAllFields
– GetItemByIdSelectedFields
– GetItemByUniqueId
http://www.SharePoSH.com
@SharePoSH
PS SharePoSH:\>
SPListItem
• Once you have an item:
– SPList.Fields or SPListItem.Fields
http://www.SharePoSH.com
@SharePoSH
PS SharePoSH:\>
SPListItem
• Reading field values
– Item[guid]
– Item[Int32]
– Item[String]
Field’s id
Field’s index
Title or Internal Name
• But it isn’t always quite that easy...
http://www.SharePoSH.com
@SharePoSH
PS SharePoSH:\>
SPListItem
• Reading field values
– SPFieldUser / SPFieldUserValue
– SPFieldLookup / SPFieldLookupValue
• LookupId
• LookupValue
$fieldName = "Assigned To"
$f = $list.Fields[$fieldName]
$fv = $f.GetFieldValue($item[$fieldName])
http://www.SharePoSH.com
@SharePoSH
PS SharePoSH:\>
SPListItem
• Similar for setting values
$new = $list.Items.Add()
$fv = new-object Microsoft.SharePoint.SPFieldUrlValue()
$new["Search URL"] = $fv
$new.Update()
http://www.SharePoSH.com
@SharePoSH
PS SharePoSH:\>
SPListItem
• Requires Update to commit
– Update()
– UpdateOverwriteVersion()
– SystemUpdate(boolean)
• true = increment item version
• false = don’t update Modified or Modified By
http://www.SharePoSH.com
@SharePoSH
PS SharePoSH:\>
SPListItem
• Common Properties
– Attachments
– Fields
– File / Folder
– HasUniqueRoleAssignments
• Common Methods
– BreakRoleInheritance
– ResetRoleInheritance
http://www.SharePoSH.com
@SharePoSH
PS SharePoSH:\>
SPFile
• SPListItem.File
• SPFolder.Files
– RootFolder property
– SPWeb.GetFolder(string)
URL of the folder
• SPWeb.GetFile(string)
http://www.SharePoSH.com
URL of the file
@SharePoSH
PS SharePoSH:\>
SPFile
• Common Properties
– Exists
– Item
– Length
– Url
File’s parent SPListItem
File size (in bytes)
http://www.SharePoSH.com
@SharePoSH
PS SharePoSH:\>
SPFile
• Common Methods
– CheckIn/CheckOut/Publish
– CopyTo(string, bool)
Destination and Overwrite
– MoveTo
– OpenBinary/OpenBinaryStream
– SaveBinary/SaveBinaryStream
http://www.SharePoSH.com
@SharePoSH
PS SharePoSH:\>
SPFile
• Download files from SharePoint via SPFile (C#)
$b = $file.OpenBinary()
$fs = New-Object System.IO.FileStream("c:\temp\test.docx", "create", "readwrite")
$bw = New-Object System.IO.BinaryWriter($fs)
$bw.Write($b)
$bw.Close()
• Just want a string?
– SPWeb.GetFileAsString(string)
http://www.SharePoSH.com
URL
@SharePoSH
PS SharePoSH:\>
Next Meeting
• TBD – Your Demos
http://www.SharePoSH.com
@SharePoSH
PS SharePoSH:\>
Resources
• PowerShell + SharePoint forum at
PowerShell.org:
– http://powershell.org/discuss/viewforum.php?f=1
2
• PowerShell Summit: April 22-24
http://powershell.org/summit
http://www.SharePoSH.com
@SharePoSH
PS SharePoSH:\>
Resources
• Save files from SharePoint document library to
File System
http://blogs.msdn.com/b/varun_malhotra/archive/2008/12/15/save-files-fromsharepoint-document-library-to-file-system.aspx
• SPListItem
http://msdn.microsoft.com/enus/library/microsoft.sharepoint.splistitem(v=office.14).aspx
• SPFile
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfile(v=office.14).aspx
http://www.SharePoSH.com
@SharePoSH
PS SharePoSH:\>
Survey!
• http://sdrv.ms/YSM470
http://www.SharePoSH.com
@SharePoSH