【问题标题】:How do I programatically add an article page to a sharepoint site?如何以编程方式将文章页面添加到共享点站点?
【发布时间】:2011-01-18 06:34:03
【问题描述】:

我的任务是从另一个 CMS 系统迁移到 SharePoint 2010。

旧系统中的数据很容易捕获,页面层次结构简单,所以我不担心。

但是,我对如何用代码创建页面完全困惑。我正在使用Microsoft.SharePoint.Client 命名空间,因为我的系统上没有安装共享点,并且我想将其编码为控制台应用程序,所以我使用的是ClientContext。 (另一方面,如有必要,我愿意进入其他解决方案)。

我的结局:要将页面上传到使用母版页的某个文件夹层次结构中,在页眉 Web 部件中具有页面标题,以及一个大的 ol' 内容可编辑 Web 部件在正文中,以便任何用户都可以来编辑内容。

到目前为止我尝试过的事情

  • 使用 FileCollection.Add() 将 aspx 文件添加到文件夹“站点页面”。这会在浏览器中呈现 html,但不会启用任何功能供用户编辑页面
  • 使用 ListItemCollection.Add() 将页面添加到站点,但我不知道需要哪些字段。我还记得它出现了一个运行时错误,说我应该使用 FileCollection.Add()
  • 上传到“网站页面”而不是“页面”
  • 还有很多其他人......我的头:(

我在网上看到的唯一可能的事情是使用PublishingPage 类型和PublishingWeb。但是,PublishingWeb 只能从 SPWeb 对象构建,这需要我在我的工作站上实际托管共享点应用程序。

如果有人能伸出援手,将不胜感激:)

【问题讨论】:

  • 属于共享点溢出^^

标签: c# sharepoint visual-studio-2010 migration


【解决方案1】:

这是我用来创建页面的一种方法。这似乎是一种比 Aquino 先生更受支持的页面创建方式。虽然这是针对 MOSS 2007 的,但我确信 2010 年也存在类似的情况。此外,我建议使用完整的对象模型创建控制台应用程序。您必须在服务器本身上运行它,但这对于迁移来说似乎不是什么大问题?这样你就不会受到任何限制。

public static void CreatePage(string url, string pageName, string title, string layoutName, Dictionary<string, string> fieldDataCollection)
    {
        var relUrl = new Uri(url);

        using (SPSite site = new SPSite(url))
        using (SPWeb web = site.AllWebs[relUrl.AbsolutePath])
        {
            if (!PublishingWeb.IsPublishingWeb(web))
                throw new ArgumentException("The specified web is not a publishing web.");
            PublishingWeb pubweb = PublishingWeb.GetPublishingWeb(web);
            PageLayout layout = null;
            string availableLayouts = string.Empty;
            foreach (PageLayout lo in pubweb.GetAvailablePageLayouts())
            {
                availableLayouts += "\t" + lo.Name + "\r\n";
                if (lo.Name.ToLowerInvariant() == layoutName.ToLowerInvariant())
                { layout = lo; break; }
            }
            if (layout == null)
                throw new ArgumentException("The layout specified could not be found.  Available layouts are:\r\n" + availableLayouts);

            if (!pageName.ToLowerInvariant().EndsWith(".aspx")) pageName += ".aspx";
            PublishingPage page = pubweb.GetPublishingPages().Add(pageName, layout);
            page.Title = title;
            SPListItem item = page.ListItem;

            foreach (string fieldName in fieldDataCollection.Keys)
            {
                string fieldData = fieldDataCollection[fieldName];
                try
                {
                    SPField field = item.Fields.GetFieldByInternalName(fieldName);

                    if (field.ReadOnlyField)
                    {
                        Console.WriteLine("Field '{0}' is read only and will not be updated.", field.InternalName);
                        continue;

                    }
                    if (field.Type == SPFieldType.Computed)
                    {
                        Console.WriteLine("Field '{0}' is a computed column and will not be updated.", field.InternalName);
                        continue;
                    }
                    if (field.Type == SPFieldType.URL)
                    {
                        item[field.Id] = new SPFieldUrlValue(fieldData);
                    }
                    else if (field.Type == SPFieldType.User)
                    {
                       // AddListItem.SetUserField(web, item, field, fieldData);
                    }
                    else
                    {
                        item[field.Id] = fieldData;
                    }
                }
                catch (ArgumentException)
                {
                    Console.WriteLine("WARNING: Could not set field {0} for item {1}.", fieldName, item.ID);
                }
            } 
            page.Update();
        }
    }

【讨论】:

  • 谢谢,我想这就是我最终要使用的。客户端查询创建和上传每个页面所花费的时间太长,而且创建 10k 新页面的来回数量会使其花费太长时间。您提供的代码看起来很棒,但是我会将页面的 html 内容放在哪里?在名为“Content”的字段中的 fieldDataCollection 中?
  • 这取决于你希望 HTML 去哪里。这是一个发布页面;它的内容实际上是在列表项列中。这就是 fieldDataCollection 集合的用途。如果您添加一个键“描述”,它将填充项目列描述等。
【解决方案2】:

如果没有实际的发布方法,我看不到创建发布页面的方法。

当您创建一个新的文章页面时,它只会在页面内创建一些 xml 参数,布局本身位于 /_catalogs/masterpage/article-XXXX.aspx 文件中。

您可以尝试下载在 Pages 文档库中创建的本机文件,了解其结构,用您的数据填充 XML,然后使用 FileCollection 将其上传回 Pages 文档库——这是我唯一的猜测。

编辑:示例文章页面

<%@ Page Inherits="Microsoft.SharePoint.Publishing.TemplateRedirectionPage,Microsoft.SharePoint.Publishing,Version=12.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" %>
<%@ Reference VirtualPath="~TemplatePageUrl" %> 
<%@ Reference VirtualPath="~masterurl/custom.master" %>
<html xmlns:mso="urn:schemas-microsoft-com:office:office" xmlns:msdt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"><head>
&nbsp;<!--[if gte mso 9]><xml>
<mso:CustomDocumentProperties>
<mso:PublishingContact msdt:dt="string">1073741823</mso:PublishingContact>
<mso:display_urn_x003a_schemas-microsoft-com_x003a_office_x003a_office_x0023_PublishingContact msdt:dt="string">System Account</mso:display_urn_x003a_schemas-microsoft-com_x003a_office_x003a_office_x0023_PublishingContact>
<mso:PublishingContactPicture msdt:dt="string"></mso:PublishingContactPicture>
<mso:PublishingContactName msdt:dt="string"></mso:PublishingContactName>
<mso:ContentTypeId msdt:dt="string">0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF390078FB5FE740F6714B9595501175ECD8F000727044016EAB3B45B9E104498E366C85</mso:ContentTypeId>
<mso:Comments msdt:dt="string"></mso:Comments>
<mso:PublishingContactEmail msdt:dt="string"></mso:PublishingContactEmail>
<mso:PublishingPageLayout msdt:dt="string">http://dmserver008/_catalogs/masterpage/ArticlePage.aspx, EstudoAndre</mso:PublishingPageLayout>
</mso:CustomDocumentProperties>
</xml><![endif]--><title>New Article</title></head>

要获取一个,点击页面库 => 内容菜单 => 发送到 => 下载副本

【讨论】:

  • 非常感谢,我已将此标记为已接受的解决方案,因为它确实适用于使用客户端上下文创建新页面。但是,我最终可能会使用服务器端 API 进行最终推送,因为每次执行 ExecuteQuery() 时客户端 API 都需要几秒钟才能导入。
【解决方案3】:

上传页面文件应该可以工作,只要您正确设置项目以及文档本身。上传文件后,您可以适当地设置内容类型和属性。如果您先手动创建一个页面,您应该能够获得一个具有所有正确设置的对象。

但是,我强烈建议您准备好开发一个控制台应用程序,该应用程序将在共享点服务器上运行,而不是依赖于 Web 服务。服务器端 api(包括 PublishingPage)往往更容易使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-23
    • 2015-01-14
    • 2010-12-10
    • 1970-01-01
    • 1970-01-01
    • 2011-10-25
    • 1970-01-01
    相关资源
    最近更新 更多