public static void createCT(string Asset, SPFolder folder, SPList targetList)
{
SqlConnection cn = new SqlConnection("server=aaa;database=aaa;user id=sa;password=sa");
SqlCommand cmd = new SqlCommand("SELECT DISTINCT TBLATTLABEL.ATTLABELNAME, COMBINEDATTR.ATTRIBUTE, COMBINEDATTR.SEARCHVALUE, COMBINEDATTR.ISCI, COMBINEDATTR.IMAGEID, COMBINEDATTR.FORMATID FROM COMBINEDATTR INNER JOIN TBLATTLABEL ON TBLATTLABEL.ATTTABLESOURCE = 'Lookup_' + COMBINEDATTR.ATTRIBUTE WHERE ISCI ='" + Asset + "'AND IMAGEID = 0 ORDER BY ATTLABELNAME", cn);
cn.Open();
SqlDataReader dr = cmd.ExecuteReader();
using (SPSite p_site = new SPSite("http://localhost:10010/"))
{
using (SPWeb p_web = p_site.OpenWeb())
{
SPContentType CustomContentType = new
SPContentType(p_web.AvailableContentTypes["Folder"],
p_web.ContentTypes, Asset +
"ContentType");int i = 0;
while (dr.Read())
{
// A string Field
p_web.Fields.Add(dr[0].ToString(),
SPFieldType.Text, false);SPFieldLink fLink1 = new SPFieldLink(p_web.Fields[dr[i].ToString()]);
CustomContentType.FieldLinks.Add(fLink1);
}
p_web.ContentTypes.Add(CustomContentType);
CustomContentType.Update();
targetList.ContentTypes.Add(CustomContentType);
targetList.Update();
SPListItem item = folder.Item;
item[
"ContentTypeId"] = CustomContentType.Id;item.Update();
dr.Close();
dr = cmd.ExecuteReader();
while (dr.Read())
{
item[dr[0].ToString()] = dr[2].ToString();
}
item.Update();
folder.Update();
}
}
}
No comments:
Post a Comment