惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

aimingoo的专栏
aimingoo的专栏
S
Securelist
博客园 - Franky
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
IT之家
IT之家
GbyAI
GbyAI
Microsoft Azure Blog
Microsoft Azure Blog
The Cloudflare Blog
云风的 BLOG
云风的 BLOG
N
News and Events Feed by Topic
AI
AI
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Schneier on Security
Schneier on Security
Attack and Defense Labs
Attack and Defense Labs
Vercel News
Vercel News
腾讯CDC
Google DeepMind News
Google DeepMind News
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
M
MIT News - Artificial intelligence
WordPress大学
WordPress大学
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
N
Netflix TechBlog - Medium
量子位
S
Schneier on Security
Hacker News: Ask HN
Hacker News: Ask HN
Cyberwarzone
Cyberwarzone
S
Security Affairs
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
N
News and Events Feed by Topic
T
Tenable Blog
PCI Perspectives
PCI Perspectives
MyScale Blog
MyScale Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Cyber Attacks, Cyber Crime and Cyber Security
W
WeLiveSecurity
N
News | PayPal Newsroom
P
Proofpoint News Feed
O
OpenAI News
C
CERT Recently Published Vulnerability Notes
B
Blog
Cisco Talos Blog
Cisco Talos Blog
Microsoft Security Blog
Microsoft Security Blog
V
Visual Studio Blog
MongoDB | Blog
MongoDB | Blog
大猫的无限游戏
大猫的无限游戏
A
Arctic Wolf
Y
Y Combinator Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Spread Privacy
Spread Privacy

博客园 - heli猫

修改infopath样式的小技巧(如何让文本框自适应文字多少?) 我在ABC公司的第一个项目 -- 编了个小故事,讲讲我理解的软件生命周期模型 用js调用ActiveX发outlook的meeting request的方法 - heli猫 - 博客园 如何解决带托管代码的infopath表单上传中的“正在删除”问题 IT项目经理经验谈 - 如何谈判 WSS、SharePoint 2003、MOSS、SharePoint 2009 大比对(5) WSS、SharePoint 2003、MOSS、SharePoint 2009 大比对(4) WSS、SharePoint 2003、MOSS、SharePoint 2009 大比对(3) WSS、SharePoint 2003、MOSS、SharePoint 2009 大比对(2) WSS、SharePoint 2003、MOSS、SharePoint 2009 大比对(1) MOSS数据库扩展设计方案 MOSS和其他系统的数据集成方式 MOSS站点的优化方案(结合kaneboy的文档) SharePoint应用场景介绍(一) 推荐一篇有用文章:创建Feature扩展SharePoint列表项或文档的操作菜单项 对象的当前状态使该操作无效 or SPListItem Update Operation is not valid due to the current state of the object 在两个不信任的域或两台独立服务器中安装SharePoint MOSS母版页制作学习笔记(二) MOSS母版页制作学习笔记(一)
通过代码 操作 sharepoint 讨论版 (转) - heli猫
heli猫 · 2010-01-20 · via 博客园 - heli猫

1) Take instance of your site collection and web:

SPSite currentSite = SPContext.Current.Site;
SPWeb currentRootWeb = currentSite.RootWeb;

2) Suppose we have the name of discussion list - "MyDiscussionList".

Get GUID of this list:

Guid myDiscussionListGUID = Guid.NewGuid();

foreach (SPList list in currentRootWeb.Lists)

{

if (list.BaseTemplate.ToString() == "DiscussionBoard" && list.Title == "MyDiscussionList")

{

myDiscussionListGUID = list.ID; // Read GUID of Discussion List

break;

}

}

Tips:

a) list.ItemCount will return all discussions and their replies

b) list.Items.Count will return only replies

3) Get your list:

SPList myDiscussionList = currentRootWeb.Lists.GetList(myDiscussionListGUID, false);

4) Create a new Discussion:

SPListItem newItem = SPUtility.CreateNewDiscussion(myDiscussionList.Items, "New Message using code");
newItem["Body"] = "My new message content using code";
newItem.Update();

5) How to read all discussions:

foreach (SPListItem folder in myDiscussionList.Folders)
{
Response.Write("Folder Name: " + folder.Name + "<BR>");
Response.Write("Folder ID: " + folder.ID + "<BR>");
Response.Write("Attachments Count: " + folder.Attachments.Count + "<BR>"); // Returns attachment count


// Code to read attachment URL.

for (int i = 0; i < folder.Attachments.Count; i++)
{
Response.Write("Attachment Url " + folder.Attachments.UrlPrefix + folder.Attachments + "<BR>");
}

// Read body of attachment

Response.Write("Body: " + folder.Fields["Body"].GetFieldValueAsText(folder["Body"]) + "<BR>");
}

6) If you want to delete a discussion:

SPListItem listItemParentToDelete = null;

foreach (SPListItem folder in myDiscussionList.Folders)
{
listItemParentToDelete = folder;

}

if (listItemParentToDelete != null)
{
listItemParentToDelete.Delete();
}


7) Loop through all discussion replies:

foreach (SPListItem listItem in myDiscussionList.Items)

{

Response.Write("Item DisplayName: " + listItem.DisplayName + "<BR>"); // Returns Title of Discussion
Response.Write("List ID: " + listItem.ID + "<BR>");
Response.Write("List Folder ID: " + listItem.Fields["Parent Folder Id"].GetFieldValueAsText(listItem["Parent Folder Id"]).ToString() + "<BR>"); // Returns ID of Parent Discussion
Response.Write("Body: " + listItem.Fields["Body"].GetFieldValueAsText(listItem["Body"]) + "<BR>");

// Create Parent List Item
int parentListID = Convert.ToInt32(listItem.Fields["Parent Folder Id"].GetFieldValueAsText(listItem["Parent Folder Id"]));
SPListItem parentListItem = lvContentItemsDiscussionsList.GetItemById(parentListID);
Response.Write("Parent List Item Name: " + parentListItem.Name + "<BR>");

// Code to Reply to a Discussion Message
SPListItem reply = SPUtility.CreateNewDiscussionReply(parentListItem);
reply["Body"] = "<div class=ExternalClass89C47CD7892B4279A8F42A65DD63AE3A><div> </div> <div>Reply to the new message<br><br> <hr> <b>From: </b>Admin<br><b>Posted: </b>Friday, July 20, 2007 4:01 AM<br><b>Subject: </b>New message<br><br> <div class=ExternalClass3D04672E599B486F9ECB76C138494708> <div>My new message content</div></div></div></div>";
reply["TrimmedBody"] = "<div class=ExternalClass677134B4EA284660B1B236824800345C><div> </div> <div>Reply to the new message<br></div></div>";
reply.Update();


// Code to delete a discussion reply
listItemToDelete = listItem;

}


8) Code to delete a discussion reply:

SPListItem listItemToDelete = null;

foreach (SPListItem listItem in myDiscussionList.Items)

{

listItemToDelete = listItem;

}

// Code to delete a discussion reply
if (listItemToDelete != null)
{
listItemToDelete.Delete();
}

Note: You need to allow unsafe updates in order to do any operation on SharePoint list directly.

currentRootWeb.AllowUnsafeUpdates = true;

原帖地址:http://www.beyondweblogs.com/post/SharePoint-Discussion-Board-Common-Operations-through-Code.aspx