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;
}
===================================================================================

CodeKeep C# Feed Novembre 6th, 2008
->
CodeKeep C# Feed Novembre 6th, 2008
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;
}

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();
}
}

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();
}
}

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();
}
}
}
}

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;
}
}

CodeKeep C# Feed Novembre 6th, 2008
CodeKeep C# Feed Novembre 6th, 2008