Archive for Novembre 6th, 2008

FindIndex

CodeKeep C# Feed Novembre 6th, 2008

Description: Various Way of using FindIndex

Link: http://www.codekeep.net/snippets/04c986f6-373d-432a-bc87-83f154a17fc1.aspx

Lambda============================================================================
      public int Position
        {
            get
            {
                if (this.HasParent)
                {
                    return _parent.Children.FindIndex(i => i == this);
                }
                else { return 0; }
            }
        }

delegate==========================================================================

       public int Position
        {
            get
            {
                if (this.HasParent)
                {
                    return _parent.Children.FindIndex(delegate(Item i) { return i == this; });
                }
                else { return 0; }
            }
        }

===================================================================================
        public int Position
        {
            get
            {
                if (this.HasParent)
                {
                    return _parent.Children.FindIndex(FindChild);
                }
                else { return 0; }
            }
        }

        private bool FindChild(Item i)
        {
            return i == this;
        }
===================================================================================

FindInex using bamda expression

CodeKeep C# Feed Novembre 6th, 2008

Description: FindInex using bamda expression

Link: http://www.codekeep.net/snippets/4a21eef1-e3f3-4371-93c8-8577d914a99b.aspx

int selectIndex = terms.FindIndex(item => item == cbTerm.SelectedText);

Write text to a file

CodeKeep C# Feed Novembre 6th, 2008

Description: Writes a text to a text file

Link: http://www.codekeep.net/snippets/6846419a-0b0e-4bac-b6c7-605d9ef57cb8.aspx

string fileName = "$filename$";
StreamWriter sw = new StreamWriter(fileName, true, Encoding.UTF8);
sw.Write("txt to Write");
sw.Close();
sw = null;

Get ListItem from Lookupfield in Sharepoint

CodeKeep C# Feed Novembre 6th, 2008

Description: Creates a SPListItem from a lookup field

Link: http://www.codekeep.net/snippets/5a6730c9-f465-4140-aa09-e7f152721618.aspx

public static SPListItem GetListItemFromLookup(SPListItem item, string lookupTableName, string key)
{
    SPFieldLookup field = item.Fields[key] as SPFieldLookup;
    if (field != null && item[key] != null)
    {
        SPFieldLookupValue fieldValue = field.GetFieldValue(item[key].ToString()) as SPFieldLookupValue;
        if (fieldValue != null)
        {
            return item.Web.Lists[lookupTableName].GetItemById(fieldValue.LookupId);
        }
    }
    return null;
}

RemoveWebPartToolbar - Procedure

CodeKeep C# Feed Novembre 6th, 2008

Description: Remove Webpart’s toolbar programmatically - procedure part

Link: http://www.codekeep.net/snippets/e92ed86e-2ffc-48c6-9853-b30cb18ecc67.aspx

protected void removeViewToolbar(ListViewWebPart wp)
        {
            // Extract view
            System.Reflection.PropertyInfo ViewProp = wp.GetType().GetProperty("View", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
            SPView spView = ViewProp.GetValue(wp, null) as SPView;

            // This forces a refresh of the views internal xml or the node's cild nodes are not populated
            string txt = spView.SchemaXml;
            System.Reflection.PropertyInfo NodeProp = spView.GetType().GetProperty("Node", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
            XmlNode node = NodeProp.GetValue(spView, null) as XmlNode;
            XmlNode tBarNode = node.SelectSingleNode("Toolbar");

            if (tBarNode != null)
            {
                tBarNode.Attributes["Type"].Value = "None";
                spView.Update();
            }
        }

Remove WebPart Toolbar in Sharepoint

CodeKeep C# Feed Novembre 6th, 2008

Description: Remove Webpart’s toolbar programmatically

Link: http://www.codekeep.net/snippets/9a2195d2-5d66-4af7-84f6-0a3bf147a1f5.aspx

#region Find the discussions web part and hide the toolbar from the web part view
                Boolean discWpExists = false;
                ListViewWebPart discwp = null;
                string pagesListName = SPUtility.GetLocalizedString("$Resources:cmscore,List_Pages_UrlName;", "cmscore", newProjectSite.Language);
                string discListName = SPUtility.GetLocalizedString("$Resources:varma,varma_sites_project_site_discussions;", "varma", newProjectSite.Language);
                SPListItem defaultPageItem = newProjectSite.Lists[pagesListName].Items[0];
                SPLimitedWebPartManager wpManager = newProjectSite.GetLimitedWebPartManager(defaultPageItem.Url, PersonalizationScope.Shared);
                foreach (System.Web.UI.WebControls.WebParts.WebPart ewp in wpManager.WebParts)
                {
                    ListViewWebPart lwp = ewp as ListViewWebPart;
                    if (lwp != null)
                    {
                        //Check if the discussions web part exists
                        if (String.Equals(lwp.DisplayTitle, discListName))
                        {
                            discwp = lwp;
                            discWpExists = true;
                            break;
                        }

                    }
                }

                if (discWpExists)
                {
                    Boolean previousState = newProjectSite.AllowUnsafeUpdates;
                    newProjectSite.AllowUnsafeUpdates = true;
                    removeViewToolbar(discwp);
                    newProjectSite.AllowUnsafeUpdates = previousState;
                }
#endregion
protected void removeViewToolbar(ListViewWebPart wp)
        {
            // Extract view
            System.Reflection.PropertyInfo ViewProp = wp.GetType().GetProperty("View", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
            SPView spView = ViewProp.GetValue(wp, null) as SPView;

            // This forces a refresh of the views internal xml or the node's cild nodes are not populated
            string txt = spView.SchemaXml;
            System.Reflection.PropertyInfo NodeProp = spView.GetType().GetProperty("Node", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
            XmlNode node = NodeProp.GetValue(spView, null) as XmlNode;
            XmlNode tBarNode = node.SelectSingleNode("Toolbar");

            if (tBarNode != null)
            {
                tBarNode.Attributes["Type"].Value = "None";
                spView.Update();
            }
        }

LDAP Entry

CodeKeep C# Feed Novembre 6th, 2008

Description: Read AD Entry using LDAP

Link: http://www.codekeep.net/snippets/0c399501-89d8-420a-82d9-93272d7ed57a.aspx

string LDAPConn = ConfigurationManager.AppSettings["LDAPConnection"].ToString();
string username = ConfigurationManager.AppSettings["LDAPUser"].ToString();
string password = ConfigurationManager.AppSettings["LDAPPassword"].ToString();

DirectoryEntry entry = new DirectoryEntry(LDAPConn, username, password);

DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(SAMAccountName=" + this.User.Identity.Name.Split(new char[] {'\\'})[1] + ")";

search.PropertiesToLoad.Add("$url$");
SearchResult result = search.FindOne();
if (result != null)
{
    foreach (string key in result.Properties.PropertyNames)
    {
        foreach (Object propValue in result.Properties[key])
        {
            if (propValue.ToString().IndexOf(MOSSAddress) >= 0)
            {
                string value = propValue.ToString();
            }
        }
    }
}

Sharepoint QuickLaunch order change

CodeKeep C# Feed Novembre 6th, 2008

Description: Code snippet to change quicklaunch navi item sort order

Link: http://www.codekeep.net/snippets/370b16cb-73f5-4fd1-bf63-1518ad73673f.aspx

SPNavigationNodeCollection quickLaunchNodes = newProjectSite.Navigation.QuickLaunch;

foreach(SPNavigationNode node in quickLaunchNodes) {
    if (node.Title.Equals("Asiakirjat"))
    {
        node.MoveToFirst(quickLaunchNodes);
        newProjectSite.Update();
        break;
    }
}

Controls in Gridview

CodeKeep C# Feed Novembre 6th, 2008

Description: Find GridView’s controls in ItemDataBound event

Link: http://www.codekeep.net/snippets/62a99265-ff6d-4346-8393-9a51f460e968.aspx

if (e.Item.ItemType == ListItemType.Item
|| e.Item.ItemType == ListItemType.AlternatingItem)
{
      Control control1 = e.Item.FindControl("MyControlID");
      if (control1 != null)
      {

      }
 }

Disable Sharepoint Quick Launch in Feature

CodeKeep C# Feed Novembre 6th, 2008

Description: Disable Quick Launch in Feature Activated Event

Link: http://www.codekeep.net/snippets/9139e74e-13a5-4830-ae75-44696b8ca4db.aspx

using (var web = (SPWeb)properties.Feature.Parent)
{
    web.AllowUnsafeUpdates = true;
    web.QuickLaunchEnabled = false;
    web.Update();
}

Next »