0

I have written two methods CreateList and UpdateList to create and update list accordingly. I need to use these methods while deploying the WSP to a Site Collection. Please advise whether I can call the methods in Feature activate method or is there any better solution/suggestion.

I tried creating a WSP file, but lists are missing when I deploy to a Site Collection. So, either I need to create the lists manually as a power user or deploy list using Visual Studio. For the latter the list is re-created every time deleting all the data. So have written my own methods to control the creation and update of lists.

Please advise.

3 Answers 3

0

The other option is create the list using list instance XML and it to the feature to created on activation. Then in feature activation method you can updated it accordingly.

Note: First it will create the list if not exists and then fires featured activated method.

0

When you deploy the WSP containing a feature using Visual Studio for the first time, it will automatically install and activate the feature. So code in feature activated event is getting called. Or if the list is created using List definition it will get created. Any subsequent deployment through Visual Studio first results is deactivating the feature, retracting and removing the solution. Then it starts all over to activate the feature (at this moment it finds a deployment conflict as the list was not removed (there wasn't any code in feature deactivated event) and giving it the permissions to "Resolve Automatically" allows it to delete the earlier instance).

However, when doing deployments outside of Visual Studio one may choose not to deactivate the feature during the retraction process and hence list will remain intact. Most often the changes to list structure are done through another feature which when activated gets hold of the list and makes changes.

Now, if you still want to go with your approach (a method to create list and another one to update it), then in feature activated code you can check whether list exists or not using SPWeb.TryGetList. If list doesn't exist it returns Null.

2
  • Hi Nadeem, thank you for your answer, it helped me a lot to understand the process. Could you also assist me to understand the best possible way (or standard) to deploy a site with custom lists (atleast 6) to production from development. What I noticed is that when I deploy the .wsp (import solution), the custom lists are not populated in target site. Thank you for your help. Commented Mar 9, 2015 at 4:57
  • @phani, please create a separate question for this issue instead of posting it in comments. That way your question will receive more attention from other users also. Commented Mar 9, 2015 at 5:36
0

In your feature receiver, you can have the method to update the list first, and if getting this list throws an exception, you create it. Below is a "pseudo" code for how you might approach it:

protected void ProcessList(){
try{ 
var list = //code to get list from SPWeb.
}

catch(exception ex){
CreateList("ListTitle");
    }

finally{

    //do something
}
}

Alternatively, you can use some methods in the object model which when executed, gets the list, if it doesn't exist, returns null, so you can check this value if null or not, if null then call the method to create the list CreatList("ListTitle");

This way, if your list exists, it will get updated, otherwise it's going to be created.

1
  • Thanks Mohamed Derhalli. Will update the code based on your suggestion. Commented Mar 4, 2015 at 7:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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