以编程方式添加SummaryLinkWebPart不显示链接

时间:2010-05-05 16:32:01

标签: sharepoint-2007

我使用下面的代码将SummaryLinkWebPart添加到页面,并添加一些指向wehbpart的链接。我现在可以在页面上看到webpart,但它里面没有任何链接。有谁知道代码有什么问题?

var wpm = web.GetLimitedWebPartManager("Pages/default.aspx", PersonalizationScope.Shared);

SummaryLinkWebPart slwp = new SummaryLinkWebPart();
    for (int counter = 0; counter < list.ItemCount; counter++)
    {
         urlField = list.Items[counter]["URL"].ToString().Split(',');

         SummaryLink link = new SummaryLink(urlField[1].Trim());
         slwp.SummaryLinkValue.SummaryLinks.Add(link);
         slwp.SummaryLinkValue.SummaryLinks[counter].OpenInNewWindow = true;
         slwp.SummaryLinkValue.SummaryLinks[counter].LinkUrl = urlField[0].Trim();
         slwp.SummaryLinkValue.SummaryLinks[counter].Description = urlField[1];
         slwp.Style = "Image on left";
         Console.WriteLine(link.LinkUrl + link.Title);
    }

    wpm.AddWebPart(slwp, lvwp.ZoneID, slwp.ZoneIndex + 1);

2 个答案:

答案 0 :(得分:2)

嗯,这似乎是黑客攻击,但解决这个问题的唯一方法是重新分配SummaryLinkValue属性的值。

slwp.SummaryLinkValue = slwp.SummaryLinkValue;

答案 1 :(得分:1)

使用

SPLimitedWebPartManager wpManager = page.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

然后完成呼叫page.Update()。像这样的东西

SPFile page = web.GetFile(string.Format("Pages/{0}", this.FileName.Replace("html", "aspx")));
SPLimitedWebPartManager wpManager = page.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
SummaryLinkWebPart webpart = new SummaryLinkWebPart();

if (!string.IsNullOrEmpty(RelatedTopic1))
    webpart.SummaryLinkValue.SummaryLinks.Add(GetSummaryLink(web, pages, RelatedTopic1));
if (!string.IsNullOrEmpty(RelatedTopic2))
    webpart.SummaryLinkValue.SummaryLinks.Add(GetSummaryLink(web, pages, RelatedTopic2));
if (!string.IsNullOrEmpty(RelatedTopic3))
    webpart.SummaryLinkValue.SummaryLinks.Add(GetSummaryLink(web, pages, RelatedTopic3));
if (!string.IsNullOrEmpty(RelatedTopic4))
    webpart.SummaryLinkValue.SummaryLinks.Add(GetSummaryLink(web, pages, RelatedTopic4));
if (webpart.SummaryLinkValue.SummaryLinks.Count > 0)
{
    wpManager.AddWebPart(webpart, "BottomPanel", 0);
    page.Update();
}