创建完整的文章页面

我们已经创建了第一页,其中将显示所有新闻文章。此页面将显示完整文章。

  1. 向 HomeController 添加一个操作方法

    [SharePointContextFilter]
    public ActionResult Aticle(int ArticleId)
    {
        User spUser = null;
    
        var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);
        FullArticle article = new FullArticle();
        using (var clientContext = spContext.CreateUserClientContextForSPHost())
        {
            if (clientContext != null)
            {
                spUser = clientContext.Web.CurrentUser;
    
                clientContext.Load(spUser, user => user.Title);
    
                clientContext.ExecuteQuery();
    
                ViewBag.UserName = spUser.Title;
    
                List lst = clientContext.Web.Lists.GetByTitle("News");
                CamlQuery queryNews = new CamlQuery();
                queryNews.ViewXml = @"<View><Query><Where><Eq><FieldRef Name='ID'/>" + "<Value Type='Number'>" + ArticleId + "</Value></Eq></Where></Query>" +
                    "<ViewFields><FieldRef Name='ID'/><FieldRef Name='Title'/><FieldRef Name='Body'/></ViewFields></View>";//
                ListItemCollection newsItems = lst.GetItems(queryNews);
                clientContext.Load(newsItems, includes => includes.Include(i => i.Id, i => i.DisplayName, i => i["Body"]));
    
                clientContext.ExecuteQuery();
    
                if (newsItems != null)
                {
                    foreach (var lstProductItem in newsItems)
                    {
                        article.Id = Convert.ToInt32(lstProductItem.Id.ToString());
                        article.Title = lstProductItem.DisplayName.ToString();
                        article.Body = lstProductItem["Body"].ToString();
                    }
                }
            }
        }
        return View(article);
    }
    
  2. 再次右键单击 Action 并创建一个具有相同名称的 View 方法名称。在我的案例中,View 将被称为 Aticle

      @model SharePointNewsAppWeb.Models.FullArticle
    
    @{
     ViewBag.Title = "Aticle";
    }
    
    <br />
    <div class="panel panel-default">
     <div class="panel-heading"><a style="font-size:20px;" href="/"><i   class="glyphicon glyphicon-chevron-left"></i> <i class="glyphicon glyphicon-home"></i>      </a></div>
     <div class="panel-heading"><h1 class="h2">@Model.Title.ToUpper()</h1></div>
     <div class="panel-body">@Html.Raw(@Model.Body)</div>
    </div>
    

这是全文页面的代码,显示了新闻文章的正文