Home > Sharepoint > Add Items to a List in Sharepoint

Add Items to a List in Sharepoint

Our List contains a couple of different Fields: User,Text,Choice,Lookup and the default Title Field. Let’s start preparing the information that we want to add into our item.

sharepoint-11

The Title Field requires a Text String, so I’m going to set up a variable containing a TextString.

PS > $Title = "Item Title"

When preparing the UserField, we need to tell Sharepoint which user we want in our field,
so we need to create an Object containing the information about the User. In this example, we will add User1.


PS > [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
PS > $SPSite = New-Object Microsoft.SharePoint.SPSite("http://wss"); $OpenWeb = $SpSite.OpenWeb("/"); $User1 = $OpenWeb.SiteUsers["BPA\User1"]; $OpenWeb.Dispose(); $SPSite.Dispose()
PS > $User1

Name             : User1
Email            : mail@apa.corp
Notes            :
LoginName        : BPA\user1
Groups           : {The New Group}
OwnedGroups      : {}
ID               : 10
Xml              : <User ID="10" Sid="S-1-5-21-2828386465-1027348
                   " Email="mail@apa.corp" Notes="" IsSiteAdmin="
RawSid           : {1, 5, 0, 0...}
Sid              : S-1-5-21-2828386465-1027348953-3114450994-1349
IsSiteAdmin      : False
IsSiteAuditor    : False
IsDomainGroup    : False
Alerts           : {}
UserToken        : Microsoft.SharePoint.SPUserToken
RegionalSettings :
ParentWeb        : Team Site
Roles            : {Contribute, Read}

The Text Field is basically the same as the Title Field.


PS > $Text = "Some Text"

The Choice Field however requires that we enter one of the choices that we created in an earlier blog post.


PS > $Choice = "Second Choice"

The last variable that we want to prepare is the LookupField Value. The List that we are currently working with has a Lookup field that retrieves
information from the "LookupList" List. We need to tell Sharepoint Which item in the "LookupList" that we want to add to our new Item.to
First Let's create a variable holding the information about the "LookupList".


PS > $SPSite = New-Object Microsoft.SharePoint.SPSite("http://wss"); $OpenWeb = $SpSite.OpenWeb("/"); $LookupList = $OpenWeb.Lists["LookupList"]; $OpenWeb.Dispose(); $SPSite.Dispose()

Next, we need to get the specific Item in the list that we want to Lookup. In this example I'll use the Where-Object to retrieve the Item that
I want. It's important to match on a unique value. If you have 2 values with the same name you could use the -and operator to get a more exact matching.


PS > $LookupItem = $LookupList.Items | Where-Object { $_.Name -match 1 }

The LookupField requires that the Item that we want to Lookup is presented in a specific way: ID;#Name
So lets prepare a variable that's presented correctly.


PS > $Lookup = ($LookupItem.ID).ToString() + ";#" + ($LookupItem.Name).ToString()

Now we can create a new item.


PS > $SPSite = New-Object Microsoft.SharePoint.SPSite("http://wss"); $OpenWeb = $SpSite.OpenWeb("/"); $List = $OpenWeb.Lists["My Custom List"]; $OpenWeb.Dispose(); $SPSite.Dispose()

PS > $Item = $List.Items.Add()
PS > $Item["Title"] = "Title"
PS > $Item["UserField"] = $User1
PS > $Item["TextField"] = $Text
PS > $Item["ChoiceField"] = $Choice
PS > $Item["LookupField"] = $Lookup
PS > $Item.Update()

sharepoint-13

Rating 3.00 out of 5
[?]
Categories: Sharepoint Tags:

Spam Protection by WP-SpamFree