I have a web part I'm creating that displays links in a metro style fed from a compatible list. I'm pretty new to share point development and was wondering how can I make sure a compatible list is available in the site (im guesssing i need to create a content type) or how can I trigger the creation of a compatible list to feed the webpart and then link the webpart to that list. I was thinking that perhaps i could add a button to the webpart properties panel that will create the list when clicked. Im not sure to be honest. Any ideas are welcome.


is this what your looking for?

Creates a new list:

SPWeb mySite = SPContext.Current.Web;

SPListTemplate template = mySite.ListTemplates["Decisions"];
mySite.Lists.Add("My Decisions", "This is a list of decisions", 

Update list item(s):

 SPSite site = SPContext.Current.Site;

 SPWeb web = site.OpenWeb();

 web.AllowUnsafeUpdates = true;

 SPList list = web.Lists["Decisions"];

 SPListItem item = list.GetItemById(Convert.ToInt32(TextBox4.Text));

 item["First Name"] = TextBox1.Text;

You can add a button to the webpart properties that triggers the code above ;)

hope it helps :)

| improve this answer | |
  • yes thats more or less what i want. I figured out how to edit the web part details pane and add custom dropdowns, buttons etc etc – Richard Banks Nov 30 '12 at 17:34
  • @almostSharepointMaster, could you explain me what is the above code doing exactly? What are TextBox1 and TextBox4? – Norbert Dec 1 '12 at 13:42
  • textbox2 is the element within the list in the list. Obiously textbox is an input like you put in name and it gets the id from name. Textbox1.text is getting whatever text is in textbox1 into the list Decisions and column "First Name". So if you want to update "First Name" colum from Matt with James then you put in textbox1 James and its now updated to "James" :) – Ali Jafer Dec 1 '12 at 13:52
  • @almostSharepointMaster, maybe it's just me, but how does this relate to the question? It was about creating lists, not updating fields. Are you sure you added this answer on the right question? – Norbert Dec 3 '12 at 9:59
  • Yup your right thats why at the top i have creating a list and then showed him how to update the list as i guess he would want to do that too – Ali Jafer Dec 3 '12 at 10:07

Best way to ensure is to create the list with object model at webpart feature activation

See, after deploying webpart you have to activate the web part feature, in that feature activation event (FeatureActivated event receiver) write your code that will create the list for you, you can also write code for list deletion at feature deactivation depending upon your choice

| improve this answer | |
  • im not really sure thats what im looking for as there could be many metro webparts so i would need many lists. – Richard Banks Nov 30 '12 at 12:21
  • you are saying you cant put a check like if (list with name does not exist) then create else proceed ? you have to keep a check that's obvious – Diptarag Nov 30 '12 at 12:23

The way I would do it:

  1. Provide a list definition of the list which will hold the links
  2. Provide a webpart with customized panel where you can choose which list (of the type above) should be used as source.

The user who adds the webpart should also create a new list for it. Creating lists and adding webparts to the page is probably not a daily activity. So I am not sure if it's worth investing any more time in a more sophisticated approach.

I hope it helped.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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