sumit
sumit

Reputation: 1

Need help in SharePoint list.

I got a code to create custom SharePoint list using Visual Studio 2010. But where can I place this code in Visual Studio 2010 was not mentioned anywhere. Can somebody help me please? I am really struggling. Here is the code:

using (SPSite oSPsite = new SPSite("http://Web URL"))
{
    oSPsite.AllowUnsafeUpdates = true;

    using (SPWeb oSPWeb = oSPsite.OpenWeb())
    {
        oSPWeb.AllowUnsafeUpdates = true;

        /* 1. create list from custom ListTemplate present within ListTemplateGalery */
        SPListTemplateCollection lstTemp = oSPsite.GetCustomListTemplates(oSPWeb);
        SPListTemplate template = lstTemp["custom template name"];
        oSPWeb.Lists.Add("List Name", "Description", template);

        /* 2. create list from sharepoint list content type (e.g. Links) */
        oSPWeb.Lists.Add("List Name", "Description", SPListTemplateType.Links);
        oSPWeb.AllowUnsafeUpdates = false;
    }
    oSPsite.AllowUnsafeUpdates = false;
}

Upvotes: 0

Views: 151

Answers (2)

Ashish Madkaikar
Ashish Madkaikar

Reputation: 129

This code can be executed from all the places where you can execute code in sharepoint depending on the requirement , here are some

  1. A webpart i.e. the code behind for the webpart
  2. It can be a part of a sharepoint page that run with code behind (http://blogs.msdn.com/b/kaevans/archive/2010/06/28/creating-a-sharepoint-site-page-with-code-behind-using-visual-studio-2010.aspx)
  3. A SharePoint Timer Job http://blogs.msdn.com/b/guruketepalli/archive/2013/02/12/10259696.aspx
  4. It can be a part of a list event handler/receiver or a web event receiver ( http://msdn.microsoft.com/en-us/library/ff407274(v=office.14).aspx )
  5. Run from some client side application

Upvotes: 0

Rob Windsor
Rob Windsor

Reputation: 6859

You could put this in a client application (Console, WinForms, WPF). The only restriction would be that the app will only work if it is executed on the SharePoint server. It will not work remotely.

Another way would be to create a SharePoint Feature and include the code in the Feature Receiver. Chapter 3 of Inside Microsoft SharePoint 2010 describes the process of building a Feature and attaching a Feature Receiver.

http://msdn.microsoft.com/en-us/library/ff872401.aspx

Upvotes: 1

Related Questions