Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have developed a web part which relies on a list to work so I've created a feature which creates the list with all the necessary columns etc.

When i delpoy to local everything works as it should, list is created etc etc. When I then upload that feature to SP2010 online the lists are not created. Do i need to use different code when a webpart is NOT being deployed but installed? Little confused.

Public Overrides Sub FeatureActivated(ByVal properties As SPFeatureReceiverProperties)
    Try
        'get the current web object.  


        ' choose your site

        'Dim currentSite As SPWeb = SPContext.Current.Web
        'local site
        'Dim site As SPSite = New SPSite("http:xxxxxxxxxxxxxxxxxxxxx")
        'live se
        Dim site As SPSite = New SPSite(" https:xxxxxxxxxxxxxxxxxxxx")
        Dim web As SPWeb = site.OpenWeb()

        Dim lists As SPListCollection = Web.Lists
        ' create new Generic list called "My List"
        ' first lets check if it exisits.
        Dim ProjectList As SPList = web.Lists.TryGetList("ProfilesTest")
        If ProjectList Is Nothing Then
            lists.Add("Profiles", "List to populate the Profiles Web part", SPListTemplateType.GenericList)
        End If


        Dim newList As SPList = web.Lists("Profiles")
        ' create Text type new column called "My Column"
        newList.Fields.Add("Name", SPFieldType.Text, True)
        newList.Fields.Add("Company", SPFieldType.Text, True)
        newList.Fields.Add("Postion", SPFieldType.Text, True)
        newList.Fields.Add("Home", SPFieldType.Text, True)
        newList.Fields.Add("Profile", SPFieldType.Note, True)
        newList.Fields.Add("Personal", SPFieldType.Note, False)
        newList.Fields.Add("Twitter", SPFieldType.Text, False)
        newList.Fields.Add("Email", SPFieldType.Text, False)
        newList.Fields.Add("Linkedin", SPFieldType.Text, False)
        newList.Fields.Add("Order", SPFieldType.Number, False)
        newList.Fields.Add("ProfileImage", SPFieldType.URL, False)

        newList.Update()

        'we can add content in here if we want
        Dim currentList As SPList = web.Lists("Profiles")
        Dim newlistItem As SPListItem = currentList.Items.Add
        newlistItem("Name") = "xxx xxx"
        newlistItem("Position") = "xxx xxx"

        newlistItem.Update()


        ' make new column visible in default view
        Dim view As SPView = newList.DefaultView
        view.ViewFields.Add("Name")
        view.ViewFields.Add("Company")
        view.Update()


    Catch

    End Try
End Sub
share|improve this question

2 Answers 2

up vote 2 down vote accepted

Instead of using

Dim site As SPSite = New SPSite(" https:xxxxxxxxxxxxxxxxxxxx") 
Dim web As SPWeb = site.OpenWeb() 

Try using (if scope of feature is Web (Site))

Dim web As SPWeb = TryCast(properties.Feature.Parent, SPWeb)

or (if scope of feature is Site (Site collection))

Dim site As SPSite = TryCast(properties.Feature.Parent, SPSite) 
Dim web As SPWeb = site.RootWeb
share|improve this answer
    
Thanks Alexander, I would prefer to go down the code route rather than CAML. You are correct though that my original code will not work in a sandbox environment. @Per thanks for your post...I can only get Site collection method to work but its creating the list in the top level site but I want it in a subsite...however when i use Web(site) method no list is being created at all. –  Stephen Oct 15 '12 at 9:35
    
if you would like to create list in a subsite you should set scope of feature to web, and use Dim web As SPWeb = TryCast(properties.Feature.Parent, SPWeb), as Per Jakobsen said. –  Alexander Oct 15 '12 at 10:12
    
In the SiteCollection version you can replace site.RootWeb with site.OpenWeb(siteRelativeUrlOfSubSite) like site.OpenWeb("subSite/subSubSite") –  Per Jakobsen Oct 15 '12 at 10:27
    
as far as I know you cannot set the scope of a feature to web. still cant get this... –  Stephen Oct 15 '12 at 11:08
    
You can set the scope of at feature to Web, but as you're deploying a web part you probably want it to stay at Site scope. I got the two mixed up in the answer, sorry. So you should use the bottom example, but with RootWeb replaced by OpenWeb(relativeUrl) –  Per Jakobsen Oct 15 '12 at 11:24

If I'm not wrong SharePoint online uses sandbox solutions. These solution has limitations. I think you cannot create list through code, but you can use CAML. See this post.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.