Moss年度隆重巨献(核心代码)
下面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 + "http://www.cnblogs.com/lindayyh/admin/file://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 + "http://www.cnblogs.com/lindayyh/admin/file://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 + "http://www.cnblogs.com/lindayyh/admin/file://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 + "http://www.cnblogs.com/lindayyh/admin/file://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 + "http://www.cnblogs.com/lindayyh/admin/file://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;
}
}
}