using System; using System.Text; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls.WebParts; using Microsoft.SharePoint; using Microsoft.SharePoint.Administration; namespace SPS2007.Research.WebParts ... { public class AjaxPart : WebPart, ICallbackEventHandler ...{ private HtmlButton btnCall; protected override void CreateChildControls() ...{ base.CreateChildControls(); btnCall = new HtmlButton(); btnCall.InnerText = "Get Tree"; btnCall.Attributes.Add("OnClick", "javascript:DisplayTree();"); this.Controls.Add(btnCall); } protected override void OnLoad(EventArgs e) ...{ if ((this.Page != null) && !this.Page.IsCallback) ...{ try ...{ ClientScriptManager manager = this.Page.ClientScript; string temp = manager.GetCallbackEventReference(this, "args", "OnComplete", "ctx", "OnMyError", true); StringBuilder sb = new StringBuilder(); sb.AppendLine("function MyCallBack(args, ctx)"); sb.AppendLine("{"); sb.AppendLine(" " + temp + ";"); sb.AppendLine("}"); sb.AppendLine("function OnComplete(args, ctx)"); sb.AppendLine("{"); sb.AppendLine(" var values = args.split('/n');"); sb.AppendLine(" for (var n in values) {"); sb.AppendLine(" var node = new MyTreeNode();"); sb.AppendLine(" node.Id = oCurrentNode.Id + "|" + values[n];"); sb.AppendLine(" node.Name = values[n];"); sb.AppendLine(" node.Children = [];"); sb.AppendLine(" oCurrentNode.Children.push(node);"); sb.AppendLine(" }"); sb.AppendLine(" DisplayTree();"); sb.AppendLine("}"); sb.AppendLine("function MyTreeNode()"); sb.AppendLine("{"); sb.AppendLine(" var Id = '';"); sb.AppendLine(" var Name = '';"); sb.AppendLine(" var Children = [];"); sb.AppendLine("}"); sb.AppendLine("");// sb.AppendLine("var nextId = 1;");// sb.AppendLine(""); sb.AppendLine("var oTree = new MyTreeNode();"); sb.AppendLine("oTree.Id = 'top';"); sb.AppendLine("oTree.Name = 'Top Tree';"); sb.AppendLine("oTree.Children = [];"); sb.AppendLine(""); sb.AppendLine("var oCurrentNode = oTree;"); sb.AppendLine("function DisplayTree()"); sb.AppendLine("{");// sb.AppendLine(" alert('display tree' + document.getElementById('Panel1'));"); sb.AppendLine(" document.getElementById('Panel1').innerHTML = GetTreeContent(1, oTree);"); sb.AppendLine("}"); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine("function GetTreeContent(level, tree)"); sb.AppendLine("{");// sb.AppendLine(" alert('get tree content:' + tree);"); sb.AppendLine(" var content = "<a href='javascript:GetTreeNodes(/"" + tree.Id + "/");'>" + tree.Name + "</a><br />";"); sb.AppendLine(" "); sb.AppendLine(" for (var node in tree.Children)"); sb.AppendLine(" {"); sb.AppendLine(" for (var i = 0; i < level; i++) {"); sb.AppendLine(" content += " ";"); sb.AppendLine(" }"); sb.AppendLine(" content += GetTreeContent(level + 1, tree.Children[node]);"); sb.AppendLine(" }"); sb.AppendLine(" "); sb.AppendLine(" return content;"); sb.AppendLine("}"); sb.AppendLine(""); sb.AppendLine("function GetCurrentTreeNode(id, tree)"); sb.AppendLine("{"); sb.AppendLine(" if (tree.Id == id)"); sb.AppendLine(" return tree;"); sb.AppendLine(" for (var node in tree.Children) {"); sb.AppendLine(" var temp = GetCurrentTreeNode(id, tree.Children[node]);"); sb.AppendLine(" if (temp != null)"); sb.AppendLine(" return temp;"); sb.AppendLine(" }"); sb.AppendLine(" return null;"); sb.AppendLine("}"); sb.AppendLine(""); sb.AppendLine("function GetTreeNodes(id)"); sb.AppendLine("{"); sb.AppendLine(" oCurrentNode = GetCurrentTreeNode(id, oTree);");// sb.AppendLine(" alert('oCurrentNode:' + oCurrentNode);"); sb.AppendLine(" MyCallBack(id);"); sb.AppendLine("}"); sb.AppendLine("function OnMyError(err)"); sb.AppendLine("{"); sb.AppendLine(" alert('error=' + err);"); sb.AppendLine("}"); manager.RegisterClientScriptBlock(base.GetType(), "MyCallBack", sb.ToString(), true); } catch(Exception ex) ...{ LogException(ex); } base.OnLoad(e); } } protected override void RenderContents(HtmlTextWriter writer) ...{ EnsureChildControls(); btnCall.RenderControl(writer); writer.WriteLine("<div id="Panel1">empty</div>"); } Implement ICallbackEventHandler Members#region Implement ICallbackEventHandler Members private string mTreeContent = "None"; public string GetCallbackResult() ...{ return mTreeContent; } public void RaiseCallbackEvent(string eventArgument) ...{ try ...{ string[] names = eventArgument.Split('|'); StringBuilder sb = new StringBuilder(); if (names.Length == 1) ...{ SPWebService service = SPWebService.ContentService; foreach (SPWebApplication app in service.WebApplications) ...{ if (app.Status != SPObjectStatus.Online) continue; if (app.ContentDatabases.Count <= 0) continue; sb.Append(app.AlternateUrls.GetResponseUrl(SPUrlZone.Default).Uri.ToString()); sb.Append(" "); } sb.Remove(sb.Length - 1, 1); mTreeContent = sb.ToString(); } else if (names.Length == 2) ...{ SPWebApplication app = SPWebApplication.Lookup(new Uri(names[1])); foreach (SPContentDatabase db in app.ContentDatabases) ...{ sb.Append(db.Name); sb.Append(" "); } sb.Remove(sb.Length - 1, 1); mTreeContent = sb.ToString(); } else if (names.Length == 3) ...{ SPWebApplication app = SPWebApplication.Lookup(new Uri(names[1])); foreach (SPSite site in app.Sites) ...{ if (site.ContentDatabase.Name != names[2]) continue; sb.Append(site.Url); sb.Append(" "); } sb.Remove(sb.Length - 1, 1); mTreeContent = sb.ToString(); } else if (names.Length == 4) ...{ SPSite site = new SPSite(names[3]); foreach (SPWeb web in site.AllWebs) ...{ if (web.Name == "") sb.Append("."); else sb.Append(web.Name); sb.Append(" "); } sb.Remove(sb.Length - 1, 1); mTreeContent = sb.ToString(); } else ...{ mTreeContent = "node1 node2 node3"; } } catch(Exception ex) ...{ LogException(ex); } } private void LogException(Exception ex) ...{ mTreeContent = "Error:" + ex.Message + " " + ex.StackTrace; } #endregion }}