简体   繁体   中英

How to create contact list items in a SharePoint 2013 contact list with PowerShell?

I'm trying to create contact list items in a SharePoint 2013 contact list with powershell. Creating items itself is not that hard:

$spWeb = Get-SPWeb -Identity http://sharepoint
$spList = $spWeb.GetList("/Contacts/Lists/Test")

$spListItem = $spList.AddItem()
$spListItem["Title"] = "New Item"

$spListItem.Update()

But setting the properties like street, telephone number, position, etc. drives me crazy. When creating a contact manually in the list via the Website and getting the details of it with PowerShell ($spList.GetItems()), all those properties are put together in a property called Xml. I know I can build the xml on my own and put it in there, but this just seems not to be the right way...

So my question is: How to create a contact item with properties like street, position, etc. correctly with PowerShell?

Update: It looks like setting properties in the xml itself does not have any impact on the item. I tried:

[XML]$a = $spListItem["Xml"];
$a.row.SetAttribute("ows_FirstName", "New Firstname")
$spListItem.Update()

But this change does not appear on the Website nor when looking at the Xml again...

Ok, finally figured it out on my own...

$spListItem["Name"] = "Name"
$spListItem["FirstName"] = "FirstName"
$spListItem["FullName"] = "FullName"
$spListItem["Email"] = "Email"
$spListItem["Company"] = "Company"
$spListItem["JobTitle"] = "JobTitle"
$spListItem["WorkPhone"] = "WorkPhone"
$spListItem["HomePhone"] = "HomePhone"
$spListItem["CellPhone"] = "CellPhone"
$spListItem["WorkFax"] = "WorkFax"
$spListItem["WorkAddress"] = "WorkAddress"
$spListItem["WorkCity"] = "WorkCity"
$spListItem["WorkState"] = "WorkState"
$spListItem["WorkZip"] = "WorkZip"
$spListItem["WorkCountry"] = "WorkCountry"
$spListItem["WebPage"] = "http://WebPage.local"
$spListItem.Update()

It is really as simple as that...

There is also as MSDN article describing how to do it in C# at https://msdn.microsoft.com/en-us/library/office/ff521580(v=office.14).aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM