首页 > 代码库 > sharepoint 知识点

sharepoint 知识点

SPListCollection转Datatable
      SPListItemCollection unprocessedItems = List.GetItems(query);
DataTable dt1 = unprocessedItems.GetDataTable(); 
Folder类型添加自定义属性

 

      

    mySite = new SPSite("http://richsql/");
                myWeb = mySite.RootWeb;

                SPDocumentLibrary spDoc = myWeb.Lists["JackDocuments"] as SPDocumentLibrary;

                SPContentTypeCollection spcontents = spDoc.ContentTypes;

                string strCTName = string.Empty;
                foreach (SPContentType spContextType in spcontents)
                {

               if (spContextType.Name == "Folder")
                    {
                        spContextType.Sealed = false;
                        spContextType.Update();
                    }
                }
 Sealed  属性设置成false就可以了.

 

多行文本取出换行

      Replace(""r"n", "<br/>")

识别为垃圾邮件

来历不明的软件发的邮件都是垃圾邮件。 

所以发邮件的时候要套个马甲。 

这个代码就是套个OutLook作掩护。 
        public bool Send(string Subject, string Body, string To, string MailFrom, string MailFromName, string MailDomain, string MailServerUserName, string MailServerPassWord) 
        { 
            try 
            { 
                MailMessage msg = new MailMessage(); 
                msg.From = new MailAddress(MailFrom, MailFromName); 
                msg.To.Add(new MailAddress(To, To)); 

                msg.Subject = Subject; 
                msg.Body = Body; 
                msg.IsBodyHtml = true; 
                msg.Priority = MailPriority.Normal; 
                msg.SubjectEncoding = System.Text.Encoding.UTF8; 
                msg.BodyEncoding = System.Text.Encoding.UTF8;

                 //以下附加头用于避免被识别为垃圾邮件
                msg.Headers.Add("X-Priority", "3"); 
                msg.Headers.Add("X-MSMail-Priority", "Normal"); 
                msg.Headers.Add("X-Mailer", "Microsoft Outlook Express 6.00.2900.2869"); 
                msg.Headers.Add("X-MimeOLE", "Produced By Microsoft MimeOLE V6.00.2900.2869"); 
                msg.Headers.Add("ReturnReceipt", "1");

                 //附加结束

                SmtpClient client = new SmtpClient(MailDomain); 
                client.UseDefaultCredentials = false; 
                client.Credentials = new NetworkCredential(MailServerUserName, MailServerPassWord); //帐号密码 
                client.DeliveryMethod = SmtpDeliveryMethod.Network; 

                client.Send(msg); 
                return true; 
            } 
            catch { return false; } 
        }

使用MOSS自带的方法发送email     

                StringDictionary messageHeaders = new StringDictionary();
                messageHeaders.Add("to", email);
                messageHeaders.Add("subject", s_subject);
                //messageHeaders.Add("from", "");
                messageHeaders.Add("content-type", "text/html");
                //messageHeaders.Add("cc", "");
                //messageHeaders.Add("bcc", "");
                SPUtility.SendEmail(this.Web, messageHeaders, s_body);

     .net方式发送email

        public static bool SendMail(string to,string subject,string body)

        {

            MailAddress from = new MailAddress("MOSS-Test1@XXX.com");

            MailAddress toUser = new MailAddress(to);

            MailMessage mailobj = new MailMessage(from, toUser);

            //完善MailMessage对象

            mailobj.Subject = subject;

            mailobj.Body = body;

            mailobj.IsBodyHtml = true;

            mailobj.BodyEncoding = System.Text.Encoding.GetEncoding("UTF-8");

            mailobj.Priority = MailPriority.Normal;

            //构建SmtpClient对象

            SmtpClient smtp = new SmtpClient("mail.xxx.com");

            try

            {

 

                smtp.Send(mailobj);

                return true;

            }

            catch

            {

                return false;

            }

        } 

     我们使用.NET类库中的APIMail的时候,我们要配置他的SMTP Server等,但是在Sharepoint里,已经提供了相关的封装的方法:
SPUtility.SendEmail(SPWeb, false, false,"to@mail.com", "MailTitle","MailBody");

上面的方法只能以To的形式来发Mail,有的时候我们需要使用CC BCC,我们可以利用该函数的另一个重载来实现:
SPUtility.SendEmail(web, messageHeaders, messageBody, appendFooter)

我们需要设置messageHeaders,代码如下:
StringDictionary headers = new StringDictionary(); 
he
aders.Add("To","To@Mail.com"); 
headers.Add("CC","CC@Mail.com"); 
headers.Add("Bcc","BBC@Mail.com"); 
headers.Add("From","From@Mail.com"); 
headers.Add("subject","The Mail Title"); 
headers.Add("content-type","text/html");

header中可以指定To CC BCC From等,当然使用上面的函数的前提是你在管理中心已经配置好了传出电子邮件的设置。

更新工作流状态

     SPList list = web.List["TestList"];
SPListItem listItem = list.GetItemById(1
);
string state = listItem.Fields["
审批状态"].InternalName;
listItem[state] = 0;    //
设置列表审批状态为已批准0表示 已批准)
listItem.SystemUpdate();    //列表使用SystemUpDate()方法来更新审批状态。



//文档库
SPDocumentLibrary docLib = web.List["TestLib"];
SPListItem libItem = list.GetItemById(1
);
string state = libItem.Fields["
审批状态"].InternalName;
libItem[state] = 0;    //
设置文档库审批状态为已批准0表示已批准)
libItem.UpdateOverwriteVersion();    //文档库使用UpdateOverwriteVersion()方法来更新审批状态。

 

item["_ModerationStatus"] = (int)SPModerationStatusType.Approved; 
item.Update(); 
这样好像就可以

string转Guid

     Guid dd = new guidstr);

Content Type

       Item[“ContentTypeId”]或者Item[“ContentType”]

显示个人信息

SPD打开根站点
在根下  _catalogs  目录有 users(用户信息表)
此表是网站集所管理的用户列表。默认为隐藏。
点右键 属性--设置-- 钩掉“在浏览器中隐藏”选项。
此时,用户表 就可以像其他列表一样操作
要实现 显示照片和姓名 职务 部门等信息
可以通过webpart 显示指定列信息,转换为XSLT视图,进行定制
注意设置筛选条件 为:   ID 列 =  [当前用户]
最终可以自由定制。

Item.SystemUpdate();

方法

更改时间

更改者

事件

更改和审核记录

订阅

properties are not demoted into documents

增加版本

SystemUpdate()

×

×

×

×

×

SystemUpdate(Bool)

×

×

×

×

False不增加

 

长时间操作,不超时

SPLongOperation oparetion = new SPLongOperation(this);

        try

        {

            oparetion.Begin();

            try

            {

                //启动工作流

                //StartInitData参数为ApproveWFAssociationData类的序列化后的string

                web.Site.WorkflowManager.StartWorkflow(listItem, workflowAssociation, StartInitData());

            }

            catch (Exception ex)

            {

                throw new Exception(ex.ToString());

            }

            oparetion.End(list.DefaultViewUrl);

        }

        finally

        {

            if (oparetion != null)

            {

                oparetion.Dispose();

            }

        }

转向

          // 转向到该list的工作流设置页面

            SPUtility.Redirect("WrkSetng.aspx?List=" + taskList.ID,

                                                        SPRedirectFlags.RelativeToLayoutsPage, HttpContext.Current);

转到错误页

                        SPUtility.TransferToErrorPage("The CreateChildControls function of the AjaxBasePart has not been called.  You probably need to add ""base.CreateChildControls()"" to the top of your CreateChildControls override.");

 

 

成员名称

说明

 

CheckUrl

Check the redirect Uniform Resource Locator (URL).  

 

Default

Use default settings for the redirect.  

 

DoNotEncodeUrl

Do not encode the redirect URL.  

 

DoNotEndResponse

Do not end the response after the redirect.  

 

RelativeToLayoutsPage

相对Layouts的地址The redirect occurs relative to the layouts page.  

 

RelativeToLocalizedLayoutsPage

相对触发的layoutsPage的地址The redirect occurs relative to the localized layouts page.  

 

Static

静态地址,非相对地址The redirect is static.  

 

Trusted

The redirect is trusted.  

 

UseSource

使用URL中的Source的地址Use the source for the redirect.  

 

http://msdn.microsoft.com/zh-cn/library/microsoft.sharepoint.utilities.spredirectflags.aspx

内容类型

SPWeb.AvailableContentTypes得到当前web所有活动的内容类型

SPWeb.ContentTypes 得到部署到当前web上的内容类型,不要用这个。

Web的用户list

web.SiteUserInfoList

AD操作

DirectoryEntry di = new DirectoryEntry("LDAP://ADServer/OU=开发部,administrator,pass, AuthenticationTypes.Secure);

广州㊣-jian(简) 说:

这句应该是最关键的了

广州㊣-jian(简) 说:

然后想在“开发部”下面增加用户,就用di.Children.Add就可以了

List附件的读取

怎么取得sharepoint表单库中的表单附件的数据,我想将附件数据单独的提取出来,但是提取的信息全是BASE64编码,有没有这方面的信息或资料,

BASE64 解码不就完了

System.Convert.FromBase64String

模拟用户代码

string siteUrl = "http://bbs.msiw.net:82/sites/sjs" ;

            //首先用管理员身份取到某个用户的Token,必须,非管理员无法模拟
            SPSite site = new SPSite(siteUrl);

            //此时用户为SHAREPOINT"system 
            Response.Write(site.RootWeb.CurrentUser);

            SPUser user = site.RootWeb.SiteUsers["saicmotorlab""user1"];

            //利用用户Token构造新的Site对象
            SPSite siteWithUser = new SPSite(siteUrl, user.UserToken);

            SPWeb webWithUser = siteWithUser.RootWeb;

            //此时用户为SAICMOTORLAB"user1 
            Response.Write( webWithUser.CurrentUser );

            SPList listWithUser = webWithUser.Lists["simeList"];

            //dother code

People Editor控件

          PeopleEditor.SelectionSet = "User,DL,SecGroup";

    代表可以选择用户,也可以选择组

工作流历史记录表

    http://cnsh-10apl1/ProjectManagement/Lists/Workflow History/

Infopath中为下拉框添加值

testNode.AppendChildElement(testNode.Prefix, "AddElm",testNode.NamespaceURI, string.Empty);

 

AJAX实现

      3:在相关MasterPage<head>里添加
<script type=‘text/javascript‘>_spOriginalFormAction = document.forms[0].action;

_spSuppressFormOnSubmitWrapper=true;</script>

4.在你写好的UserControlPage_load事件里添加

if (this.Page.Form != null)
             {
                 string formOnSubmitAtt = this.Page.Form.Attributes["onsubmit"];
                 if (formOnSubmitAtt == "return _spFormOnSubmitWrapper();")
                 {
                     this.Page.Form.Attributes["onsubmit"] =

"_spFormOnSubmitWrapper();";
                 }
             }
             ScriptManager.RegisterStartupScript(this,

typeof([UserControlClassName]), "UpdatePanelFixup", "_spOriginalFormAction =

document.forms[0].action; _spSuppressFormOnSubmitWrapper=true;", true);

Person or Group字段允许多人时,通过sdk对象读取用户信息

List<SPUser> users = new List<SPUser>(); 
List<SPGroup> groups = new List<SPGroup>(); 

SPFieldUserValueCollection values = (SPFieldUserValueCollection)item["
人员或组栏"]; 

foreach (SPFieldUserValue value in values) 
{ 
if (value.User != null) 
{ 
users.Add(value.User); 
} 
else 
{ 
SPGroup group = web.Groups.GetByID(value
.LookupId); 
groups.Add(group); 
foreach (SPUser user in group.Users) 
{ 
users.Add(user); 
} 
} 
}

SPFieldLookupValueCollection  values = (SPFieldLookupValueCollection )item["Users"];

SPFieldLookupValueCollection values = item["Users "].ToString();
string initUsers = "";
foreach (SPFieldLookupValue value in
 values)
{

int
 valueID = value.LookupId;
string
 valueTitle = value.LookupValue;

}

代码启动工作流

          获取spworkflowmanager 获取spworkflowassociation,针对一个item启动

Item中添加附件

     if (fileUploadAttach.HasFile)
                {
                    Stream stream = fileUploadAttach.PostedFile.InputStream;
                    stream.Position = 0;
                    byte[] buffer = new byte[stream.Length];
                    stream.Read(buffer, 0, (int)stream.Length);
                    itemNew.Attachments.Add(fileUploadAttach.PostedFile.FileName, buffer);
                    itemNew.Update();
                    itemNew.ParentList.Update();
                    stream.Close();
                }

CAML查找Lookup

     <FieldRef Name="GMP" LookupId="TRUE"/>

         <Value Type="Lookup">1</Value>

用户型

         "<FieldRef Name=""User"" LookupId=""TRUE""/>" +

          "<Value Type=""User"">" + user.ID + "</Value>" +

WEB页面显示word

          如果想让web页面显示出word文档要加

Response.ContentType = "application/msword";

Response.AddHeader("Content-Disposition","attachment;filename=" +

HttpUtility.UrlEncode(file.FILENAME).Replace("+","%20"));// 防止乱码

Response.Charset="UTF-8";

Response.ContentEncoding=System.Text.Encoding.Default;

Response.BinaryWrite(文件流);

Response.End();

多文件上传

          SPFileCollection destFiles =

    siteCollection.AllWebs["Destination_Site"]

    .Folders["Destination_DocLib"].Files;

 

foreach (SPFile srcFile in srcFolder.Files)

{

    if (srcFile.TimeLastModified <

        Convert.ToDateTime("12/5/2002 12:00:00 AM"))

    {

        string destURL = destFiles.Folder.Url + "/" + srcFile.Name;

        byte[] binFile = srcFile.OpenBinary();

                                    destFiles.Add(destURL, binFile, true);

    }

}

工作流中权限

          先HybridDictionary一个对象叫task1permission,然后task1permission.Add(taskProps.AssignedTo, SPRoleType.Contributor),最后将task1permission赋值给task的SpecialPermissions

检查权限

public void CheckPermissions (SPBasePermissions permissionMask)

检查当前用户是否有permissionMask的权限,如果没有返回一个UnauthorizedAccessException的异常。

Bool havePermission = SPList.DoesUserHavePermissions(SPBasePermissions permissionMask)

添加权限

SPList DTTasks = web2.Lists["Document Tasks"];

SPRoleAssignment roleAssignForMember = new SPRoleAssignment((SPPrincipal)newGroup); //为newGroup这个组分配权限

SPRoleDefinition roleDefForMemberr = web2.RoleDefinitions["DTMembers"];

roleAssignForMember.RoleDefinitionBindings.Add(roleDefForMemberr);

 

if (!DTTasks.hasUniqueRoleAssignments)//如文件夹没有独立的权限

    {

       DTTasks.BreakRoleInheritance(false);      //删除文件夹继承的权限 true删除的同时继承false删除的同时不继承

     }

DTTasks.RoleAssignments.Add(roleAssignForMember);

TTasks.Update();

 

无法查询文件夹下的item,只能查找根目录的解决办法:

query.ViewAttributes = " Scope=‘Recursive‘ "

 

Member name

Description

 

Default

Show only the files and subfolders of a specific folder.  

 

FilesOnly

Show only the files of a specific folder.  

 

Recursive

Show all files of all folders.  

 

RecursiveAll

Show all files and all subfolders of all folders.  

The SPViewScope enumeration is used with the Scope property of the SPView class.

EventHandler中的Adding Updating等操作如何赋值

public override void ItemAdding(SPItemEventProperties properties)

{
    // Demo1: 
在新建时取用户输入的值并作修改
    string newValue =http://www.mamicode.com/ "新的值是:" + properties.AfterProperties["Title"].ToString();
    properties.AfterProperties.ChangedProperties.Add("Title"
, newValue);
}

如何从SPAttachmentCollection中获取到SPfile

SPAttachmentCollection attachments = item.Attachments;

          if (attachments.Count > 0)

             {

               for (int i = 0; i < attachments.Count; i++)

                  {

                     string url = attachments.UrlPrefix + attachments[i];//得到附件的共同前缀再加上附件的名称

                     SPFile file = web.GetFile(url);

                     string customerName = item["Customer Name"].ToString().Split(‘#‘)[1];

                     file.CopyTo(properties.WebUrl + "/CRM Document/" + customerName + "/04 Customer Scoring/" + attachments[i], true);

                            }

如何查看SharePoint未知错误

MOSS开发自定义功能后,出现错误页面只显示出现未知错误的提示,查看很不方面,通过下面方法就可以直接在出错页面查看错误信息。

 

修改Web应用程序根目录上的Web.config文件中的两个地方:
查找以下位置并更改即可(红色为更改前后的值)
一、MaxControls="200" CallStack="false"   改为  MaxControls="200" CallStack="true"
二、customErrors mode="On"  改为  customErrors mode="Off"

 

Webpart出错,无法打开页面,contents=1 ,会转到webpart管理页面,把出问题的删掉

URL后面加入“?&toolpaneview=2”打开设计页

或者加“DisplayMode=Design”

            http://cnsh-10vms1/_layouts/spcontnt.aspx?&url=/_catalogs/wp/forms/editForm.aspx这页面是维护页面

超链接或图片类型栏的值,对应的对象模型为SPFieldUrlSPFieldUrlValue,可以这样读取:

SPFieldUrlValue value = http://www.mamicode.com/new SPFieldUrlValue(item["URL"].ToString());
       Console.WriteLine(value.Description);
       Console.WriteLine(value.Url);

SPFieldUrl fieldUrl = (SPFieldUrl)item.Fields["URL"];
SPFieldUrlValue value = http://www.mamicode.com/(SPFieldUrlValue)fieldUrl.GetFieldValue(item["URL"].ToString());
Console.WriteLine(value.Description);
Console.WriteLine(value.Url);

赋值

            SPFieldUrl fieldUrl = (SPFieldUrl)supplierNew.Fields[dgi.Cells[0].Text];

    SPFieldUrlValue fieldUrlValue =http://www.mamicode.com/ new SPFieldUrlValue();

     fieldUrlValue.Url = web.Url + "/" + supplierDocument.RootFolder.Url + "/" + supplierNew["Title"].ToString() + "-" + supplierNew.ID;

    fieldUrlValue.Description = dgi.Cells[0].Text;

    supplierNew[dgi.Cells[0].Text] = fieldUrlValue;

 

将子网站移动到主网站???

修改子网站下的网站栏到网站集下解决了,直接到WSS_Content数据库里面,修改表ContentTypes的Scope字段,特别方便,暂时没发现出错误~

SharePointLookUp字段的使用

                赋值

如何在代码中使用lookup字段

 

 

  

对于lookup字段在可视界面时,感觉挺方便,但是在写代码的时候,对它进行赋值的时候,就感觉很麻烦。不知道哪位高手对这个赋值有更好的方法,谢谢。 
方法一(听11同学说的) 
SPFieldLookupValueCollection lookupValues; 

lookupValues = (SPFieldLookupValueCollection)listItem["MyLookupField"]; 

lookupValues.Add(new SPFieldLookupValue(1, "SomeLookupValue")); 
listItem["MyLookupField"] = lookupValues; 

listItem.Update();


这方法感觉赋值就是受罪,写这么多代码。 
方法二。直接用 
listItem["MyLookupField"] =1,2,3 
但这此方法要知道字符串所对应的Id。,有什么最有效的方法。。请教高手

实际使用如下代码(经过测试):

 

                                 SPWeb web = SPContext.Current.Web;

            SPList customerList = web.Lists["Customer"];

            SPQuery query = new SPQuery();

            query.Query = "<Where><Eq><FieldRef Name=‘Title‘/><Value Type=‘Text‘>" + DropDownList1.SelectedItem.Text + "</Value></Eq></Where>";

            SPListItemCollection customerC = customerList.GetItems(query);

            int cu1 = customerC[0].ID;

            SPList projectList = web.Lists["Project"];

            SPListItem projectNew = projectList.Items.Add();

            projectNew["Customer"] = cu1;

            projectNew["Title"] = txtboxProjectName.Text;

            projectNew.Update();

LookUpCollection的赋值

SPFieldLookupValueCollection lvsGDE = new SPFieldLookupValueCollection();

            foreach (ListItem item in lbGDECurrent.Items) //lbGDECurrent为下拉框

            {

                SPFieldLookupValue lvGDE = new SPFieldLookupValue();

                lvGDE.LookupId = int.Parse(item.Value);

                lvsGDE.Add(lvGDE);

            }

Item[Lookups] = lvsGDE;

LookUpUser读取

    如果LookUp不允许多选的时候,Item["LookUp"]得到的是一个string

    String C = (item["Created By"] == null) ? "" : item["Created By"].ToString().Remove(0, item["Created By"].ToString().IndexOf(";#") + 2);

    否则是一个SPFieldLookUpValueCollection

    用户类型为SPFieldUserValueCollection

    List<SPUser> Users = new List<SPUser>();
            List<SPGroup> Groups = new
 List<SPGroup>();

            using (SPSite Site = new SPSite("http://windbell"
))
            
 {
                SPWeb Web = Site.RootWeb;
                SPList List = Web.Lists["
测试列表"];
                SPItem Item = List.Items[0
];

                SPFieldUserValueCollection Values = (SPFieldUserValueCollection)Item["
用户和用户组"];

                foreach (SPFieldUserValue Value in
 Values)
                
 {
                    if (User != null
)
                    
 {
                        SPUser User = Value.User;
                        Users.Add(User);
                    }
                    else

                     {
                        SPGroup Group = Web.Groups.GetByID(Value.LookupId);
                        Groups.Add(Group);
                        Users.AddRange(Group.Users);
                    }
                }
            }

 

定义一个Group

        SPGroupCollection groups = oWebsite.SiteGroups;

    SPUser user = oWebsite.Users["xxx""xxxxxx"];

    SPMember member = oWebsite.Users["xxxxx""xxx"];

groups.Add("TestGroup", member, user, "Description");

 

将一个Group分配进一个Role

                                if (SPWeb.HasUniqueRoleAssignments == true)//web是否有自己的角色分配,有True
                    {
                        SPWeb.AllowUnsafeUpdates = true;

                        SPRoleAssignment roleAssign = new SPRoleAssignment((SPPrincipal)SPSite.RootWeb.SiteGroups["Style Resource Readers"]);

                        SPRoleDefinition roleDef = SPWeb.RoleDefinitions["Open Access"];
                        roleAssign.RoleDefinitionBindings.Add(roleDef);

                        SPWeb.RoleAssignments.Add(roleAssign);
                    }

读取User Profile

ServerContext spcontext2 = ServerContext.GetContext(site);

                    UserProfileManager profileManager = new UserProfileManager(spcontext2);

                    string userName = member.Name;

                    SPListItem newMember = projectMember.Items.Add();

                    newMember["User Name"] = member.Name;

                    newMember["Project"] = Project;

                    newMember["Group"] = newGroup.Name;

                    newMember["E-mail Address"] = member.Email;

                    if (profileManager.UserExists(userName))

                    {

                       UserProfile profile = profileManager.GetUserProfile(userName);

                       newMember["Business Phone"] = profile["BusinessPhone"].Value;

                       newMember["Mobile Phone"] = profile["CellPhone"].Value;

                       newMember["Fax"] = profile["Fax"].Value;

                       //newMember["Notes"] = member[PropertyConstants.CellPhone].

                       newMember["SkypeMSN"] = profile["SkypeMSN"].Value;

                       newMember["Principalship"] = profile["Principalship"].Value;

                       newMember["Location"] = profile["Location"].Value;

                    }

                    newMember.Update();

关闭EventHandler

this.DisableEventFiring();//关闭EventHandler,防止多重触发

//save changes

addedItem.Update();

this.EnableEventFiring();

sharepoint 知识点