承接MOSS各种工作流开发 联系人:王先生.电话:18618405729QQ:252385878 QQ群:41696750 MSN:wanghao-3@hotmail.com

导航

Moss年度隆重巨献(核心代码)

                                          在SharePoint发展到今天这个图飞猛进的时代,我们还能做点什么?经过拉半年的不但努力,感觉自己也有所成就,把自己研究的心得和宝贵的代码。我觉得学习就是一个Share的过程。你会,一定要带动大家会,这样你才能理解的更深刻。只有在Show自己的时候,才能更好的去帮助别人。在国内,SharePoint的应用也只是皮毛,希望热爱MOSS的哥们一起努力把。把MOSS的潜能都挖掘出来,这样我们这些热爱SharePoint的同胞们才能更好的。更快的,学习到更先进的技术,让老外们来看我们的“中文”的资料,(嘿嘿,发点牢骚,看E文,头疼,眼睛疼)。。让他们来向我们请教。。。。(希望真正的高手们,Show,Share)....看啦很多“高手的Blog”,都是一些鸡胫。。真正有含量的东西没有。。。。我当时学习MOSS的时候,找一些有用的资料的时候,可是害苦我了。难找啊。。。。找到很多好东西时候,一看,培训交钱,(一天最少也是 1K以上。。。),其实讲的也就是入门级别的东西。。。。。。哎。。。。。
下面Demo介绍 关于怎么样把我们写好的一个WebPart 用代码完成的安装,然后自动启用的到指定页面指定的位置过程,,,前段时间研究拉KaneBoy的QP包装器,然后把它破解。。下次公布代码。。
下面代码很简单。大家看看就应该能明白。。不明白的提出来。。(用最简单的代码,来实现复杂的应用)还是那句话,代码现跑起来,调试容易理解。。。。
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Web;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.IO;
using System.Reflection;
using System.Diagnostics;
using Microsoft.SharePoint.WebPartPages;
namespace SetupWebPartTest
{
    class Program
    {
        /// <summary>
        /// 站点IIS绝对路径
        /// </summary>
        static string SiteAbsPath = "";

        static void Main(string[] args)
        {
              ModifyTrust();

           SetupLibrary(@"C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\WebPartTest\WebPartTest\bin\Debug");

            SetupWebpart(@"C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\WebPartTest\WebPartTest\bin\Debug");


        }

        private static void ModifyTrust()
        {

            SPSite site = new SPSite(@"http://lh-vmpc:6060/");

            SiteAbsPath = site.WebApplication.IisSettings[SPUrlZone.Default].Path.FullName;

            string webconfig = SiteAbsPath + "\\web.config";
            XmlDocument config = new XmlDocument();
            config.Load(webconfig);

            //修改信任级别为“<trust level="Full" originUrl="" />”
            XmlNode trust = config.SelectSingleNode("configuration/system.web/trust");
            trust.Attributes["level"].Value = "Full";

            //
            XmlNode sess = config.SelectSingleNode("configuration/system.web/pages");
            sess.Attributes["enableSessionState"].Value = "true";

            XmlNode sessMo = config.SelectSingleNode("configuration/system.web/httpModules");
            XmlNode add = config.CreateElement("add");
            XmlAttribute attr1 = config.CreateAttribute("name");
            attr1.Value = "Session";
            XmlAttribute attr2 = config.CreateAttribute("type");
            attr2.Value = "System.Web.SessionState.SessionStateModule";
            add.Attributes.Append(attr1);
            add.Attributes.Append(attr2);
            sessMo.AppendChild(add);

            config.Save(webconfig);
        }

        private static void SetupLibrary(string path)
        {
            if (Directory.Exists(path))
            {
                string[] files = Directory.GetFiles(path);

                foreach (string file in files)
                {
                    string filename = Path.GetFileName(file);

                    string targetFile = SiteAbsPath + "\\bin\\" + filename;

                    if (!File.Exists(targetFile))
                    {
                        File.Copy(file, targetFile, true);
                    }
                }
            }
        }

 

        private static void SetupWebpart(string path)
        {
            string webpartTitle = "WebPartTitle";
            string webpartDesc = "WebPartDesc";
            string webpartZoneID = "";
            string webpartSiteID = "";
            string webpartXml = "";

            if (Directory.Exists(path))
            {
                string[] dlls = Directory.GetFiles(path);

                foreach (string dll in dlls)
                {
                    if (dll.ToLower().IndexOf(".dll") != -1)
                    {
                        Assembly assembly = Assembly.LoadFile(dll);

                        Type[] types = assembly.GetTypes();
                        SiteAbsPath = @"C:\Inetpub\wwwroot\wss\VirtualDirectories\6060";
                        //拷贝dll文件
                   //     File.Copy(dll, SiteAbsPath + "\\bin\\" + Path.GetFileName(dll));
                       
                        #region 在web.config中组册为安全组件
                        //string safe = "<SafeControl Assembly=\"" + types[0].Module.Name + "\"" + " Namespace=\"" + types[0].Namespace + " TypeName=\"*\" Safe=\"True\" />";
                        XmlDocument config = new XmlDocument();
                        config.Load(SiteAbsPath + "\\web.config");

                        XmlNodeList tmpAsembly = config.SelectNodes("configuration/SharePoint/SafeControls[SafeControl/@Assembly='" + Path.GetFileNameWithoutExtension(types[0].Module.Name) + "']");

                        if (tmpAsembly == null || tmpAsembly.Count == 0)
                        {
                            XmlNode SafeControlNodes = config.SelectSingleNode("configuration/SharePoint/SafeControls");

                            XmlNode SafeControlNode = config.CreateNode(XmlNodeType.Element, "SafeControl", "");
                            XmlAttribute attr;

                            attr = config.CreateAttribute("", "Assembly", "");
                            attr.Value = Path.GetFileNameWithoutExtension(types[0].Module.Name);
                            SafeControlNode.Attributes.Append(attr);

                            attr = config.CreateAttribute("", "Namespace", "");
                            attr.Value = types[0].Namespace;
                            SafeControlNode.Attributes.Append(attr);

                            attr = config.CreateAttribute("", "TypeName", "");
                            attr.Value = "*";
                            SafeControlNode.Attributes.Append(attr);

                            attr = config.CreateAttribute("", "Safe", "");
                            attr.Value = "True";
                            SafeControlNode.Attributes.Append(attr);

                            SafeControlNodes.AppendChild(SafeControlNode);

                            config.Save(SiteAbsPath + "\\web.config");
                        }
                        #endregion

                        #region 在当前站点启用webpart
                        foreach (Type t in types)
                        {
                            //判断是否是webpart
                            try
                            {
                                System.Web.UI.WebControls.WebParts.WebPart tmp = (System.Web.UI.WebControls.WebParts.WebPart)Activator.CreateInstance(t);

                                LH.EIP.ControlPackage.WebPartClassAttribute MyAttribute = (LH.EIP.ControlPackage.WebPartClassAttribute)Attribute.GetCustomAttribute(t, typeof(LH.EIP.ControlPackage.WebPartClassAttribute));

                                webpartTitle = "未知的描述信息";
                                webpartDesc = "未知的描述信息";

                                if (MyAttribute != null)
                                {
                                    webpartTitle = MyAttribute.Title;
                                    webpartDesc = MyAttribute.Description;
                                    webpartZoneID = MyAttribute.ZoneID;
                                    webpartSiteID = MyAttribute.SiteID;
                                }

                                //增加站点的webpart gallery描述,可能一个dll文件中存在多个webpart
                                string gallery = "";
                                gallery = gallery + "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
                                gallery = gallery + "<webParts>";
                                gallery = gallery + "<webPart xmlns=\"http://schemas.microsoft.com/WebPart/v3\">";
                                gallery = gallery + "<metaData>";
                                gallery = gallery + "<type name=\"" + t.AssemblyQualifiedName + "\"/>";
                                gallery = gallery + "<importErrorMessage>Cannot import this web part.</importErrorMessage>";
                                gallery = gallery + "</metaData>";
                                gallery = gallery + "<data>";
                                gallery = gallery + "<properties>";
                                gallery = gallery + "<property name=\"Title\" type=\"string\">" + webpartTitle + "</property>";
                                gallery = gallery + "<property name=\"Description\" type=\"string\">" + webpartDesc + "</property>";
                                gallery = gallery + "</properties>";
                                gallery = gallery + "</data>";
                                gallery = gallery + "</webPart>";
                                gallery = gallery + "</webParts>";

                                webpartXml = gallery;

                                //-------------------------------------------------------------------------------
                                byte[] buffer1 = new UTF8Encoding().GetBytes(gallery);

                                SPSite site = new SPSite(@"http://lh-vmpc:6060/");
                                SPWeb web = site.OpenWeb();
                                SPFileCollection collection2 = web.Files;
                                SPFile file = collection2.Add("_catalogs/wp/" + t.FullName + ".webpart", buffer1);

                                SPListItem item = file.Item;

                                item["用户组"] = "中科软件Webpart库";
                                item.Update();


                                AddWebpartToPage(@"http://lh-vmpc:6060/", webpartXml, "left", 4, "WebPartTest");
                            }
                            catch (Exception ee)
                            {
                                string error = ee.Message;
                            }
                        }
                        #endregion
                    }
                }
            }
        }


        private static bool AddWebpartToPage(string TargetSiteUrl, string webPartXml, string ZoneID, int ZoneIndex, string Title)
        {
            bool flag = false;
            SPLimitedWebPartManager manager = null;

            if (TargetSiteUrl == "" || webPartXml == "" || ZoneID == "")
            {
              //  err = "001";
                return false;
            }

            try
            {
                //重新启动IIS

                Process p = new Process();
                p.StartInfo.FileName = "iisreset.exe";
                p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                p.Start();
                p.WaitForExit();

                SPSite sites = new SPSite(TargetSiteUrl);
                SPWeb web = sites.RootWeb;

                manager = web.GetLimitedWebPartManager(@"http://lh-vmpc:6060/default.aspx", System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
                StringReader input = new StringReader(webPartXml);
                XmlTextReader reader2 = new XmlTextReader(input);
                string err = "1";
                System.Web.UI.WebControls.WebParts.WebPart webpart = manager.ImportWebPart(reader2, out err);
                webpart.ChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.None;
                manager.AddWebPart(webpart, ZoneID, ZoneIndex);

                web.Close();

            //    this.SetTip("\"" + Title + "\"" + "安装完成");

                flag = true;

            }
            catch (Exception exception)
            {
               // err = exception.Message;
                flag = false;
            }
            finally
            {
                if (manager != null)
                {
                    manager.Dispose();
                }
            }
            return flag;
        }
    }
}

 

posted on 2007-07-13 14:12  A A  阅读(2418)  评论(19编辑  收藏  举报