简体   繁体   中英

Programmatically Add New Choice to Choice Column in SharePoint List

I'm working with a client and trying to make it so that when a button is pressed to open new Fiscal Year (FY) forms, it also adds that FY value as a choice option in a Document Library column. I also am trying to set the default to that new value.

I'm getting the error below. 错误信息

Here's my current code. Is it simply a configuration issue? Or am I doing something n00by with my code?

Imports Microsoft.SharePoint
...
Partial Class _Default
    Inherits System.Web.UI.Page
...
Protected Sub SaveNew_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SaveNew.Click

 ...

                'Added 6/12/2019 - Testing new FY options in Document Upload Form
                Using site As New SPSite("http://sptest/spsite/default.aspx")
                    Using web As SPWeb = site.OpenWeb()
                        Dim leadLst As SPList = web.Lists("Document Library")
                        Dim col As SPFieldChoice = CType(leadLst.Fields("Fiscal Year"), SPFieldChoice)
                        col.Choices.Add(FY.Text)
                        col.DefaultValue = FY.Text
                        col.update()
                        leadLst.update()
                    End Using
                End Using
                'End Added 6/12/2019

 ...

End Sub

When you are creting an object of SPSite, you need to pass the URL of Site Collection only not with .aspx page Convert your SPSite object creation line from

 Using site As New SPSite("http://sptest/spsite/default.aspx")

to

Using site As New SPSite("http://sptest/spsite/")

if spsite is your subsite and above code do not work then convert that line to

Using site As New SPSite(" http://sptest/ ")

And then get SPWeb object for the spsite subsite

Let me know if this helps or not?

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