Contents tagged with CodeSnippets

  • Register site in Sharepoint programatically

            /// <summary>
            /// Eventhandler that invokes the RegisterInSiteDirectory method. Will only run on the SPS server 
            /// with SPS admin as current identity.
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button1_Click(object sender, System.EventArgs e)
            {
                Hashtable itemData = new Hashtable();
                itemData.Add("CustomCategory", "SomeData");
                this.RegisterInSiteDirectory("SiteDirectory", "Sites", "Generated Site Name", "Some Description", "http://sps2003base:8080/sites/genepool", itemData);
            }
            /// <summary>
            /// Registers a site in a given site directory and enlists site for search in the same manner as 
            /// the Sharepoint Site Directory Area UI
            /// </summary>
            /// <param name="siteDirectoryAreaName">Name of the Site Directory area.</param>
            /// <param name="siteDirectoryName">Name of the Site Directory List.</param>
            /// <param name="title">Site Title to be used in the list and search entry.</param>
            /// <param name="description">Site Description to be used in the list and search entry.</param>
            /// <param name="siteUrl">Site Description to be used in the list and for crawling.</param>
            /// <param name="itemData">Custom site metadata used in the list and included in portal content search.</param>
            public void RegisterInSiteDirectory(string siteDirectoryAreaName, string siteDirectoryName, string title, string description, string siteUrl, Hashtable itemData)
            {
                SPSite portalSite = new SPSite("http://sps2003base:8080");
                SPWeb siteDirectoryArea = portalSite.AllWebs[siteDirectoryAreaName];
                SPList siteDirectoryList = siteDirectoryArea.Lists[siteDirectoryName];
                
                // Create new item in the list
                SPListItem item = siteDirectoryList.Items.Add();
                
                // Set default and required values
                item["SiteTitle"] = title;
                item["SiteURL"] = siteUrl;
                item["Description"] = description;
                
                // Add custom metadata
                foreach(DictionaryEntry entry in itemData)
                {
                    item[entry.Key.ToString()] = entry.Value;
                }
                
                item.Update();
                // Register the site for search
                Uri siteUri = new Uri("http://sps2003base:8080"); 
                TopologyManager tm = new TopologyManager(); 
                PortalSite site = tm.PortalSites[siteUri]; 
                PortalContext portalContext = PortalApplication.GetContext(site); 
                AreaManager.SuggestDeepCrawl( portalContext, 
                    title, 
                    description, 
                    siteUrl, 
                    "" /* LargeIconUrl */, 
                    "" /* SmallIconUrl */, 
                    "$$$default$$$" /* Index Catalog */, 
                    "$$$default$$$" /* Index Scope */); 
            }

  • Customize Outlook Web Access display elements

            private string OwaDisplayScript
            {
                get
                {
                    StringBuilder sb = new StringBuilder();
                    sb.Append("<script language='JavaScript'>function window.onload()");
                    sb.Append("{");
                    sb.Append("var form = document.forms[0];");
                    sb.Append("if( form.document.readyState == 'complete' )");
                    sb.Append("{");
                    sb.Append("var oStyles = form.document.createStyleSheet();");
                    sb.Append("oStyles.addRule( '.tblFolderBar', 'display:none' );");
                    sb.Append("oStyles.addRule( '.trToolbar', 'display:none' );");
                    sb.Append("}");
                    sb.Append("}</script>");
                    return sb.ToString();
                }
            }

  • QueryStringPageViewer WebPart for Sharepoint

        /// <summary>
        /// Mads Haugbø Nissen 2003(c)
        /// Objectware AS
        /// Norway
        /// 
        /// http://weblogs.asp.net/mnissen
        /// 
        /// The QueryStringPageViewer provides the same functionality as the regular PageViewerWebPart
        /// but allows providing a selection of querystring parameters from the page that hosts this webpart
        /// to the page viewed in the QueryStringPageViewer.
        /// </summary>
        [DefaultProperty("ContentLink"),
        ToolboxData("<{0}:QueryStringPageViewer runat=server></{0}:QueryStringPageViewer>"),
        XmlRoot(Namespace="http://www.objectware.no/Lawyer/Sharepoint/Webparts/QueryStringPageViewer")]
        public class QueryStringPageViewer : Microsoft.SharePoint.WebPartPages.WebPart
        {
            private string _contentLink = "";
            [Browsable(true)]
            [Category("Content")]
            [DefaultValue("")]
            [WebPartStorage(Storage.Shared)]
            [FriendlyName("The url of the content to show.")]
            [Description("Supply Querystring params to be passed like this: Page.aspx?Param1&Param2&Param3")]
            public string ContentLink
            {
                get
                {
                    return this._contentLink;
                }
                set
                {
                    this._contentLink = value;
                }
            }
            
            protected string BaseUrl
            {
                get
                {
                    int endIndex = this.ContentLink.IndexOf("?");
                    if(endIndex != -1)
                        return this.ContentLink.Substring(0, endIndex + 1);
                    else
                        return "";
                }
            }
            /// <summary>
            /// Render this Web Part to the output parameter specified.
            /// </summary>
            /// <param name="output"> The HTML writer to write out to </param>
            protected override void RenderWebPart(HtmlTextWriter output)
            {
                string url = this.UrlWithQueryStringGet();
                if(url.Length > 0)
                {
                    string name = "QueryStringPageViewer_" + this.UniqueID;
                    output.AddAttribute(HtmlTextWriterAttribute.Id, name, false);
                    output.AddAttribute(HtmlTextWriterAttribute.Name, name, false);
                    output.AddAttribute(HtmlTextWriterAttribute.Width, "100%", false);
                    output.AddAttribute(HtmlTextWriterAttribute.Height, "100%", false);
                    output.AddAttribute(HtmlTextWriterAttribute.Height, "100%", false);
                    output.AddAttribute(HtmlTextWriterAttribute.Src, url, false);
                    output.AddAttribute("ddf_src", url, false);
                    output.AddAttribute("frameBorder", "0", false);
                    output.RenderBeginTag(HtmlTextWriterTag.Iframe);
                    output.RenderBeginTag(HtmlTextWriterTag.Div);
                    output.Write("IFrames not supported by this browser");
                    output.RenderEndTag();
                    output.RenderEndTag();            
                }
                else
                {
                    Control c = new LiteralControl(string.Format("To link to content, <a href=\"javascript:MSOTlPn_ShowToolPaneWrapper('{0}','{1}','{2}');\">open the tool pane</a> and then type a URL in the ContentLink text box with QueryString parameters formatted like this: 'Page.aspx?Param1&Param2'.", 1, 129, this.ID)); 
                    c.RenderControl(output);
                }
            }
            
            private ArrayList QueryStringKeysGet()
            {
                int startIndex = this.ContentLink.IndexOf("?");
                ArrayList queryStrings = new ArrayList();
                if(startIndex > -1 && this.ContentLink.Length > startIndex + 1)
                {
                    string queryStringRaw = this.ContentLink.Substring(startIndex + 1);
                    if(queryStringRaw.IndexOf("&") != -1)
                    {    
                        string[] tokens = queryStringRaw.Split(new char[]{'&'});
                        for(int i = 0; i < tokens.Length; i++)
                            queryStrings.Add(tokens[i].ToLower());
                    }
                    else
                    {
                        queryStrings.Add(queryStringRaw.ToLower());
                    }
                }
                return queryStrings;
            }
            protected virtual string UrlWithQueryStringGet()
            {
                ArrayList queryStringsToPass = this.QueryStringKeysGet();
                string url = this.BaseUrl;
                
                for(int i=0; i < Page.Request.QueryString.Count; i++)
                {
                    string queryStringKey = Page.Request.QueryString.Keys[i];
                    string queryStringValue =  Page.Request.QueryString[i];
                    if(queryStringsToPass.IndexOf(queryStringKey.ToLower()) != -1)
                    {
                        url += queryStringKey + "=" + queryStringValue + "&";
                    }
                }
                
                url = url.TrimEnd(new char[]{'&'});
                return url;
            }
        }
    
    <?xml version="1.0" encoding="utf-8"?>
    <WebPart xmlns="http://schemas.microsoft.com/WebPart/v2" >
        <Title>QueryStringPageViewer</Title>
        <Description>Passes querystring parameters to the page in the viewer.</Description>
        <Assembly>Objectware.Lawyer.Sharepoint</Assembly>
        <TypeName>Objectware.Lawyer.Sharepoint.WebParts.QueryStringPageViewer</TypeName>
        <!-- Specify initial values for any additional base class or custom properties here. -->
        <ContentLink xmlns="http://www.objectware.no/Lawyer/Sharepoint/Webparts/QueryStringPageViewer"></ContentLink>
    </WebPart>

  • ExceptionXmlPublisher for Microsoft.ApplicationBlocks.ExceptionManagement

    using System;
    using System.Xml;
    using System.Collections.Specialized;
    using System.IO;
    namespace Microsoft.ApplicationBlocks.ExceptionManagement
    {
        /// <summary>
        /// Summary description for ExceptionXmlPublisher.
        /// </summary>
        public class ExceptionXmlPublisher : IExceptionXmlPublisher
        {
            void IExceptionXmlPublisher.Publish(XmlDocument ExceptionInfo, NameValueCollection ConfigSettings)
            {
                string filename;
                if (ConfigSettings != null)
                {
                    filename = ConfigSettings["fileName"];
                }
                else
                {
                    filename = @"C:\ErrorLog.xml";
                }
                XmlDocument xmlTarget = new XmlDocument();
                FileStream fs;
                if(File.Exists(filename))
                {
                    fs =  File.Open(filename, FileMode.Open, FileAccess.Read);
                    try
                    {
                        xmlTarget.Load(fs);
                    }
                    catch
                    {
                        xmlTarget = new XmlDocument();
                        xmlTarget.AppendChild(xmlTarget.CreateXmlDeclaration("1.0", "UTF-8", "yes"));
                        xmlTarget.AppendChild(xmlTarget.CreateElement("RuntimeExceptions"));
                    }
                    finally
                    {
                        fs.Close();
                    }
                }
                else
                {
                    xmlTarget.AppendChild(xmlTarget.CreateXmlDeclaration("1.0", "UTF-8", "yes"));
                    xmlTarget.AppendChild(xmlTarget.CreateElement("RuntimeExceptions"));
                }
                
                XmlDocumentFragment docFrag = xmlTarget.CreateDocumentFragment();
                docFrag.InnerXml = ExceptionInfo.OuterXml;
                XmlNodeList stackTraceNodes = docFrag.SelectNodes("//StackTrace");
                foreach(XmlNode node in stackTraceNodes)
                {
                    node.InnerXml = "<![CDATA[" + node.InnerXml + "]]>";
                }
                
                xmlTarget.DocumentElement.AppendChild(docFrag);
                
                fs = File.Open(filename, FileMode.Create, FileAccess.Write);
                try
                {
                    xmlTarget.Save(fs);
                }
                catch
                {}
                finally
                {
                    fs.Close();
                }
                
            }
        }
    }