0

我的程序获取列表共享点的版本历史元素的数据并将数据放入 txt
文件中。输出“名称”列表项和“位置”。一切正常,但不幸的是输出“名称”非常糟糕。在我看来这一行的问题

string strFirstName = Convert.ToString(objVersion["Name"]);

输出不良:

Name 4049;#Zabiyakin Makar ,#STALT\Zabiyakin,# Zabiyakin@stalt.ru,#,#Zabiyakin Makar Location В Name 4049;#Zabiyakin Makar ,#STALT\Zabiyakin,# Zabiyakin@stalt.ru,#,#Zabiyakin Makar位置 首页

我想要输出:

名称 Zabiyakin Makar 位置 В 名称 Zabiyakin Makar 位置 首页

代码程序:

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.IO;
namespace ControlTest.Layouts.ControlTest
{
    public partial class ApplicationPage1 : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //string ListUrl = "/Deps/sup/Lists/Whereabouts";
            SPList objList = SPContext.Current.Web.Lists["Whereabouts"];
            SPQuery objQuery = new SPQuery();
            objQuery.Query = "<Where><Gt><FieldRef Name=\"ID\"/><Value                 
            Type=\"Counter\">0 </Value></Gt></Where>";
            SPListItemCollection objItemColl = objList.GetItems(objQuery);  
            foreach (SPListItem objItem in objItemColl) 
            {
                SPListItemVersionCollection objVerisionColl = objItem.Versions;
                if (objVerisionColl.Count > 1)
                {
                    foreach (SPListItemVersion objVersion in objVerisionColl)  
                    {
                        int versionID = objVersion.VersionId; 
                        DateTime timeofcreation = objVersion.Created;  
                        string strVersionLabel = objVersion.VersionLabel;
                        SPListItem objLstItm = objVersion.ListItem;
                        string strFirstName = Convert.ToString(objVersion["Name"]);
                        //string strFirstName = 
                        ((SPFieldUserValue)objVersion["Name"]).User.Name;
                        string strPlace = Convert.ToString(objVersion["location"]);
                        System.IO.TextWriter writer = new 
                        StreamWriter(@"C:\custommenuoutput.txt", true);
                        writer.WriteLine(string.Format("Name: {0} location: {1}", 
                        strFirstName, strPlace));
                        writer.Close();

                    }

                }
              }
           }
     }
  }

如何做出好的输出?请帮我 !

4

1 回答 1

1

您需要使用 SPUserFieldValue 来获取友好名称。请参阅下面的示例。

SPSite site = new SPSite("http://spvm");
SPWeb web = site.OpenWeb();
SPList list = web.Lists["DemoList"];
SPListItem item = list.GetItemById[1];
SPFieldUserValue objUserFieldValue = new SPFieldUserValue(web, item["User"].ToString());

获得此对象 SPFieldUserValue ( objUserFieldValue) 后,您可以通过以下方式检索该用户的每个属性。

objUserFieldValue.User.LoginName;
objUserFieldValue.User.Name;
objUserFieldValue.User.ID;
objUserFieldValue.User.Groups;
objUserFieldValue.User.Roles;
objUserFieldValue.User.Email;
objUserFieldValue.User.Sid;
objUserFieldValue.User.UserToken;
于 2012-05-19T22:36:18.920 回答