在SharePoint 2010中使用SPMetal创建的LoginName(ID)

时间:2012-06-07 16:36:48

标签: linq sharepoint-2010

我正在使用SP2010中的OOB博客网站。我正在使用SPMetal为帖子列表(以及其他)生成实体类。我已经使用了一个parameters.xml文件来获取我需要的其他列,默认情况下不包含这些列。

我想做的其中一件事就是获取用户的“我的网站”网址。我能够相对容易地使用CAML做到这一点。但是我需要使用Linq。我无法弄清楚如何获取作者字段的登录ID(即domain \ id)。我查看了联系人内容类型,似乎没有任何帮助。

是否有人遇到此问题或获得了使用SPMetal的用户的登录ID?

1 个答案:

答案 0 :(得分:0)

如果您使用SPMetel.exe创建Posts列表的实体,并且如果在假设字段类型为用户的帖子列表中,则自动返回两个类似LookupId和LookupValue的方法。

在我的情况下:我已将promoterid作为我的实体中Posts列表中的字段名称,有两种方法

    private System.Nullable<int> _promoterId;
    private string _promoter;
    [Microsoft.SharePoint.Linq.ColumnAttribute(Name="promoterid", Storage="_promoterId", FieldType="User", IsLookupId=true)]
    public System.Nullable<int> PromoterId {
    get {
        return this._promoterId;
    }
    set {
        if ((value != this._promoterId)) {
            this.OnPropertyChanging("PromoterId", this._promoterId);
            this._promoterId = value;
            this.OnPropertyChanged("PromoterId");
        }
    }
}

[Microsoft.SharePoint.Linq.ColumnAttribute(Name="promoterid", Storage="_promoter", ReadOnly=true, FieldType="User", IsLookupValue=true)]
public string Promoter {
    get {
        return this._promoter;
    }
    set {
        if ((value != this._promoter)) {
            this.OnPropertyChanging("Promoter", this._promoter);
            this._promoter = value;
            this.OnPropertyChanged("Promoter");
        }
    }
}

比我能够使用linq查询后

     SPWeb oWebsiteRoot = SPContext.Current.Web;
     EntitiesDataContext objent = new EntitiesDataContext(oWebsiteRoot.Url);
     EntityList<PostsItem> evnitems = objent.GetList<PostsItem>("Posts");
     var i =  from item in evnitems
              where item.PromoterId == SPContext.Current.Web.CurrentUser.ID 
             select item;