SharePoint,让代码飞一会儿

获取列表创建者名字: 
jsom: var author = item.get_item("Author").get_lookupValue();
 csom:SP.FieldUserValue Author = ListItems["Author"] as SP.FieldUserValue;                     if (Author != null)                     {                       string  Creator = Convert.ToString(Author.LookupValue);                     }
服务器对象模型: SPListItem getitme = SPContext.Current.ListItem;                     string getAuthor = getitme["Author"].ToString(); //创建人                     LabelCreatUser.Text = getAuthor.Split('#')[1];   //创建人姓名,


SharePoint 操作userprofile,注意,必须是用户自己查看自己的信息;在代码中,用管理员账户没有权限获取其他用户信息
using Microsoft.SharePoint;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;
string username= SPContext.Current.Web.CurrentUser.LoginName;
SPSecurity.RunWithElevatedPrivileges(delegate
2 {
3 using (SPSite mySite = new SPSite(SPContext.Current.Site.ID))
4 {
5
6 SPServiceContext myContext = SPServiceContext.GetContext(mySite);
7
8 UserProfileManager myProfile = new UserProfileManager(myContext);
9 UserProfile user = myProfile.GetUserProfile(userName);
10 if (user != null)
11 {
12 string property1 = user["UserProfileProperty"].Value;
13 }
14 }
15 });
复制代码

 

 

 

 

查看User Profile的名称和显示名称

首先这段代码需要用到3个DLL,分别是Microsoft.SharePoint.dll; Microsoft.Office.Server.UserProfiles.dll; Microsoft.Office.Server.dll;然后在代码中将其引用:

1 using Microsoft.SharePoint;
2 using Microsoft.Office.Server.UserProfiles;
3 using Microsoft.Office.Server;

主要代码如下:

复制代码
 1 using (SPSite mySite = new SPSite("http://yourServerName/"))
2 {
3 SPServiceContext context = SPServiceContext.GetContext(mySite);
4 ProfileSubtypeManager psm = ProfileSubtypeManager.Get(context);
5 ProfileSubtype ps = psm.GetProfileSubtype(ProfileSubtypeManager.GetDefaultProfileName(ProfileType.User));
6 ProfileSubtypePropertyManager pspm = ps.Properties;
7 foreach (ProfileSubtypeProperty pro in pspm.PropertiesWithSection)
8 {
9 Console.WriteLine(pro.Name + "\t" + pro.DisplayName);
10 }
11 Console.ReadLine();
12 }
复制代码

 

 

禁止匿名访问,才能获取当前用户名

第一种方法:

以下是代码片段:
System.Web.HttpContext.Current.User.Identity.Name.ToString();

  这种方法不用获取当前的Site和Web信息,是全局使用的帐号信息。此方法获取的帐号信息是带域的。

  第二种方法:

以下是代码片段:
    string username = "";
  SPSite site = new SPSite("http://test");
  SPWeb web = site.OpenWeb();
  username = web.CurrentUser.LoginName.ToString();

 

代码创建列表

SPSite site=new Site(SPContext.current.web.url)
SPWeb web = site.OpenWeb();
web.AllowUnsafeUpdates = true;
GuidnewListId = web.Lists.Add("对象模型创建的列表", "描述信息", SPListTemplateType.GenericList);
//获取创建好的列表对象
SPListnewList = web.Lists[newListId];
//在快速启动菜单上显示
newList.OnQuickLaunch = true;
newList.Update();
web.AllowUnsafeUpdates = false;
//释放资源
web.Close()
web.Dispose()
site.Close();
site.Dispose();


创建栏
第一种:
                                            SPFieldNumber fldEmpID = (SPFieldNumber)newList.Fields.CreateNewField(
                                            SPFieldType.Number.ToString(), “sss”);
                                            var innername = newList.Fields.Add(fldEmpID);
第二种:
 newList.Fields.Add("YesNoEdit", SPFieldType.Boolean, false);
            SPFieldBoolean YesNoEdit = newList.Fields["YesNoEdit"] as SPFieldBoolean;
            YesNoEdit.DefaultValue = "0";
            YesNoEdit.Update();
            newList.Fields.Add("Schedule", SPFieldType.Number, false);
            newList.Fields.Add("ApprovalStatus", SPFieldType.Boolean, false);
            SPFieldBoolean ApprovalStatus = newList.Fields["ApprovalStatus"] as SPFieldBoolean;
            ApprovalStatus.DefaultValue = "0";
            ApprovalStatus.Update();

            newList.Fields.Add("ImportantNode", SPFieldType.Boolean, false);
            SPFieldBoolean ImportantNode = newList.Fields["ImportantNode"] as SPFieldBoolean;
            ImportantNode.DefaultValue = "0";
            ImportantNode.Update();

            newList.Fields.Add("ShowHome", SPFieldType.Boolean, false);
            SPFieldBoolean ShowHome = newList.Fields["ShowHome"] as SPFieldBoolean;
            ShowHome.DefaultValue = "0";
            ShowHome.Update();


string strPrimaryCol = relatedList.Fields.AddLookup("Customer ID", lookupList.ID, true);
                        SPFieldLookup primaryCol = (SPFieldLookup)relatedList.Fields.GetFieldByInternalName(strPrimaryCol);

                        primaryCol.LookupField = lookupList.Fields["ID"].InternalName;
                        primaryCol.Indexed = true;
                        primaryCol.RelationshipDeleteBehavior = SPRelationshipDeleteBehavior.Restrict;
                        primaryCol.Update();


                        // Create the secondary columns.

                        string strFirstNameCol = relatedList.Fields.AddDependentLookup("First Name", primaryCol.Id);
                        SPFieldLookup firstNameCol = (SPFieldLookup)relatedList.Fields.GetFieldByInternalName(strFirstNameCol);
                        firstNameCol.LookupField = lookupList.Fields["First Name"].InternalName;
                        firstNameCol.Update();

                        string strLastNameCol = relatedList.Fields.AddDependentLookup("Last Name", primaryCol.Id);
                        SPFieldLookup lastNameCol = (SPFieldLookup)relatedList.Fields.GetFieldByInternalName(strLastNameCol);
                        lastNameCol.LookupField = lookupList.Fields["Last Name"].InternalName;
修改显示名
lastNameCol.Title = "你好啊"; lastNameCol.Update()
添加到默认视图: spList.DefaultView.ViewFields.Add(lookup),这样添加就无效
 SPView view = spList.DefaultView;
                    
                    view.ViewFields.Add(lookup);
                    view.Update();


 

 列表中创建新文件夹

SPList list = web.Lists[listId];
list.EnableFolderCreation = true;
SPListItem item = list.AddItem("", SPFileSystemObjectType.Folder);
item["Title"] = "FolderTitle";
item.Update();

 

创建文档库

SPSite site = new SPSite(siteurl);
SPListItem item = site.AllWebs["网站名"].Lists["列表名"].Folders.Add("文档库URL", SPFileSystemObjectType.Folder);
item["Name"] = your folder name;
site.RootWeb.AllowUnsafeUpdates = true;
item.Update();
site.RootWeb.AllowUnsafeUpdates = false;

上传文件到指定文档库

复制代码
FileStream fs = new FileStream(direct, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
Byte[] filecontents = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
SPFolder folder = site.RootWeb.GetFolder("文档库url");
string foldername = 上传上去的文档URL;
site.RootWeb.AllowUnsafeUpdates = true;
SPFile sofle = folder.Files.Add(foldername, filecontents, true);
site.RootWeb.AllowUnsafeUpdates = false;
复制代码
 
 
 

posted on 2015-05-12 21:20  !无名之辈  阅读(231)  评论(0)    收藏  举报