title: Creating Content Item via Code in Orchard CMS
body: <p>I need to create content item using code. i know there is built in way but i want my own functionality in module. I tried the code but it gives me error. All related code is given below. Content part name is "Course" and Content Item name is "Courses"</p>
  
  <p>"Object reference not set to an instance of an object."</p>
  
  <pre><code>[HiddenInput(DisplayValue = false)]
  public int Id {
  get { return ContentItem.Id; }
  }
  
  **Controller**
  
    public ActionResult Create()
      {            
          var course = _orchardService.ContentManager.New("Courses");
          dynamic model = _orchardService.ContentManager.BuildEditor(course);
  
          return View((object)model);                                  
      }
  
  
      [HttpPost, ActionName("Create")]
      public ActionResult CreatePOST(string idl)
      {
  
          var contentItem = _contentManager.New("Courses");
  
          _contentManager.Create(contentItem, VersionOptions.Draft);
  
          dynamic model = _contentManager.UpdateEditor(contentItem, this);
  
          _contentManager.Publish(contentItem);
  
          _orchardService.Notifier.Information(new LocalizedString("Your content has been created."));
  
          var adminRouteValues = _contentManager.GetItemMetadata(contentItem).AdminRouteValues;
  
          return RedirectToRoute(adminRouteValues);
      }
  
  
      public ActionResult Index(PagerParameters pagerParameters, CourseSearchVM search)
      {
          //this is displaying only published content
          var courseQuery = _contentManager.Query&lt;CoursePart&gt;().List().ToList();
          // Project the query into a list of customer shapes
          var coursesProjection = from course in courseQuery
                                    select Shape.course
                                    (
                                      Id: course.Id,
                                      Name: course.Name,
                                      Description: course.Description
                                    );
  
          // The pager is used to apply paging on the query and to create a PagerShape
          var pager = new Pager(_siteService.GetSiteSettings(), pagerParameters.Page, pagerParameters.PageSize);
          // Apply paging
          var coures = coursesProjection.Skip(pager.GetStartIndex()).Take(pager.PageSize);
          // Construct a Pager shape
          var pagerShape = Shape.Pager(pager).TotalItemCount(courseQuery.Count());
          // Create the viewmodel
          var model = new CourseIndexVM(coures, search, pagerShape);
          return View(model);
      }
  
  
  **CoursePart Model**
  
  public class CoursePart : ContentPart&lt;CoursePartRecord&gt;
  {
      public string Name
      {
          get { return Record.Name; }
          set { Record.Name = value; }
      }
  
      public string Description
      {
          get { return Record.Description; }
          set { Record.Description = value; }
      }
  }
  
  public class CoursePartRecord : ContentPartRecord
  {
      public virtual string Name { get; set; }
      public virtual string Description { get; set; }
  }
  
  
  **Create.cshtml**
  
  @{ Layout.Title = "Edit Course"; }
  
  @using(Html.BeginFormAntiForgeryPost())
  {
  @Display(Model)
  }
  
   **Course.cshtml**
  
   @model Course.Models.CoursePart
   &lt;fieldset&gt; 
  &lt;div class="editor-label"&gt;@Html.LabelFor(x =&gt; x.Name)&lt;/div&gt;
  &lt;div class="editor-field"&gt;
      @Html.EditorFor(x =&gt; x.Name)
      @Html.ValidationMessageFor(x =&gt; x.Name)
  &lt;/div&gt;
  
  &lt;div class="editor-label"&gt;@Html.LabelFor(x =&gt; x.Description)&lt;/div&gt;
  &lt;div class="editor-field"&gt;
      @Html.EditorFor(x =&gt; x.Description)
      @Html.ValidationMessageFor(x =&gt; x.Description)
  &lt;/div&gt;
  &lt;/fieldset&gt;
  </code></pre>