About Me

Tuesday, August 27, 2013

Add Document library breadcrumb thru webpart.


using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace BreadCrumb
{
    [Guid("2717b54c-cefb-466e-aef9-e2de91a9ee2f")]
    public class BreadCrumb : System.Web.UI.WebControls.WebParts.WebPart
    {
        public BreadCrumb()
        {
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            Label Label1 = new Label();

            string currentFolder = "";
            string strTmp = "";
            string strLink = "";

            if (Page.Request.QueryString["RootFolder"] != null)
            {

                string[] a;

                currentFolder = Page.Request.QueryString["RootFolder"];

                currentFolder = currentFolder.Replace("%2f", "/");

                currentFolder = currentFolder.Replace("%20", " ");

                a = currentFolder.Split('/');

                for (int i = (a.Length - 1); i >= 0; i--)
                {

                    if (strTmp.Length > 0)

                        strTmp = " > " + strTmp;

                    if (i != (a.Length - 1))
                    {

                        //Iteratively gets the link for each folder.

                        for (int x = 1; x <= i; x++)
                        {

                            strLink = strLink + a[x];

                            if (x != i)

                                strLink = strLink + "/";

                            strLink = strLink.Replace("/", "%2f");

                            strLink = strLink.Replace(" ", "%20");

                        }

                        strTmp = "" + a[i] + "" + strTmp;

                    }

                    else

                        strTmp = a[i] + strTmp;

                    //Reset Link to ""

                    strLink = "";

                    if (i == 2) break;

                }

            }



            Label1.Text = "
" + strTmp + "
";
            this.Controls.Add(Label1);
           
            // TODO: add custom rendering code here.
            // Label label = new Label();
            // label.Text = "Hello World";
            // this.Controls.Add(label);
        }
       
    }
}

Tree view webpart for all sites and Lists:


using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Diagnostics;
namespace SiteCollectionLists
{
[Guid("47bb4600-1a80-42c8-95a4-dd0c8b2722b9")]
[CLSCompliant(false)]
public class SiteCollectionLists : WebPart
{
TreeView tvListnLib;
public SiteCollectionLists()
{
}
protected override void RenderWebPart(HtmlTextWriter output)
{
try
{
tvListnLib.RenderControl(output);
}
catch (Exception Ex)
{
EventLog.WriteEntry(Ex.Message.ToString(), "", EventLogEntryType.Error);
}
}
protected override void
CreateChildControls()
{
try
{ // TODO: add custom rendering code here.
tvListnLib = new TreeView();
tvListnLib.ShowLines = true;
tvListnLib.EnableViewState = true;
tvListnLib.CollapseAll();
tvListnLib.ID = "lstname";
PopulateList();
Controls.Add(tvListnLib);
}
catch (Exception Ex)
{
EventLog.WriteEntry(Ex.Message.ToString(), "", EventLogEntryType.Error);
}
}
public void PopulateList()
{
SPWeb objWeb = SPControl.GetContextSite(Context).RootWeb;
try
{
TreeNode nodeRoot = null;
tvListnLib.Nodes.Clear();
if ((objWeb.Exists) && (objWeb.Title != null) && (objWeb.Url != null))
{
nodeRoot = new TreeNode(objWeb.Title);
tvListnLib.NodeStyle.CssClass = "ms-input";
tvListnLib.Nodes.Add(nodeRoot);
nodeRoot.NavigateUrl = objWeb.Url + "/default.aspx";
FillWeb(objWeb, nodeRoot);
SPListCollection lstColForCurrSite = objWeb.GetListsOfType(SPBaseType.GenericList);
FillList(lstColForCurrSite, nodeRoot, "List");
SPListCollection DLColForCurrSite = objWeb.GetListsOfType(SPBaseType.DocumentLibrary);
FillList(DLColForCurrSite, nodeRoot, "Document Library");
}
}
catch (Exception Ex)
{
EventLog.WriteEntry(Ex.Message.ToString(), "", EventLogEntryType.Error);
}
}
public void FillWeb(SPWeb webCurr, TreeNode objTreeNode)
{
SPListCollection lstColForCurrSite = null;
SPListCollection DLColForCurrSite = null;
try
{
foreach (SPWeb objWeb in webCurr.GetSubwebsForCurrentUser())
{
TreeNode newTreeNode = null;
try
{
if (objWeb.Exists)
{
newTreeNode = new TreeNode(objWeb.Title);
objWeb.Lists.ListsForCurrentUser = true;
objTreeNode.ChildNodes.Add(newTreeNode);
newTreeNode.NavigateUrl = objWeb.ServerRelativeUrl+"/default.aspx";
newTreeNode.CollapseAll();
lstColForCurrSite = objWeb.GetListsOfType(SPBaseType.GenericList);
FillList(lstColForCurrSite, newTreeNode, "List");
DLColForCurrSite = objWeb.GetListsOfType(SPBaseType.DocumentLibrary);
FillList(DLColForCurrSite, newTreeNode, "Document Library");
}
}
catch (Exception Ex)
{
EventLog.WriteEntry(Ex.Message.ToString(), "", EventLogEntryType.Error);
}
FillWeb(objWeb, newTreeNode);
//call recurcive function for all subsite.
}
}
catch (Exception Ex)
{
EventLog.WriteEntry(Ex.Message.ToString(), "", EventLogEntryType.Error);
}
finally
{
if (webCurr != null)
webCurr.Dispose();
}
}
public void FillList(SPListCollection lstColCurrWeb, TreeNode newTreeNode, string Type)
{
try
{
int lstCount = 0;
SPList lstNode;
TreeNode ListNode = new TreeNode(Type);
newTreeNode.ChildNodes.Add(ListNode);
if (Type == "List")
ListNode.NavigateUrl = lstColCurrWeb.Web.Url + "/_layouts/viewlsts.aspx?BaseType=0";
else
ListNode.NavigateUrl = lstColCurrWeb.Web.Url + "/_layouts/viewlsts.aspx?BaseType=1";
TreeNode tnNewList = null;
for (lstCount = 0; lstCount < lstColCurrWeb.Count; lstCount++)
{
lstNode = lstColCurrWeb[lstCount];
tnNewList = new TreeNode(lstNode.Title, lstNode.ParentWebUrl);
ListNode.ChildNodes.Add(tnNewList);
tnNewList.NavigateUrl = lstNode.DefaultViewUrl;
tnNewList.CollapseAll();
}
}
catch (Exception Ex)
{
EventLog.WriteEntry(Ex.Message.ToString(), "", EventLogEntryType.Error);
}
}
}
}

SharePoint help for c# coding

Get a copy of dll in GAC (or) add Reference to a dll in GAC

Using this we cannot copy the dll. Only uninstall option is available.
1. To view the available dll using the naked eye follow the steps
Dot net have a dll file Shfusion.dll which is a Assembly Cache Viewer. It is located in the following path.
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\shfusion.dll
2. uninstall the dll using the following command in the run dialog box.
regsvr32 -u C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\shfusion.dll
3. Now type assembly in the Run dialog box.
4. Now you will see the folder view of the GAC. copy the dll you want.

Note:
To get back to the previous state of view register the Shfusion dll using the following command
5. regsvr32 C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\shfusion.dll

More

Run SharePoint Code using Security Elevated

SPSecurity.CodeToRunElevated objElevatedCode = null;
objElevatedCode = new SPSecurity.CodeToRunElevated(
delegate
{
using (SPWeb web = new SPSite(strCurrentWebURL).OpenWeb())
{
///Your Code
}
} );
SPSecurity.RunWithElevatedPrivileges(objElevatedCode);

Get all the Lists Name from all sites and sub sites.

DataTable Table = null;
SPSiteDataQuery Query = new SPSiteDataQuery();
Query.Lists = "";
Query.Query = "";
Query.Webs = "";
Query.ViewFields = "";
SPWeb web = new SPSite("URL").OpenWeb();
Table = web.GetSiteData(Query);
Table.TableName = "row";
MessageBox.Show("Count : " + Table.Rows.Count.ToString());
dataGridView1.DataSource = Table;

Get the Assembly information from Dll name:

Assembly custAssembly = Assembly.LoadWithPartialName("ADOB");
string strAssembly = custAssembly.FullName.ToString();

Get Directory Stracture

DirectoryInfo dir = new DirectoryInfo(path);
DirectoryInfo[] subDirs = dir.GetDirectories();
FileInfo[] files = dir.GetFiles();

Return more than one value through function/method in C# (OUT)

class OutExample
{
// Splits a string containing a first and last name separated
// by a space into two distinct strings, one containing the first name and one containing the last name
static void SplitName(string fullName, out string firstName, out string lastName)
{
// NOTE: firstName and lastName have not been assigned to yet. Their values cannot be used.
int spaceIndex = fullName.IndexOf(' ');
firstName = fullName.Substring(0, spaceIndex).Trim();
lastName = fullName.Substring(spaceIndex).Trim();
}
static void Main()
{
string fullName = "Yuan Sha";
string firstName;
string lastName;
// NOTE: firstName and lastName have not been assigned yet. Their values may not be used.
SplitName(fullName, out firstName, out lastName);
// NOTE: firstName and lastName have been assigned, because the out parameter passing mode guarantees it.
System.Console.WriteLine("First Name '{0}'. Last Name '{1}'", firstName, lastName);
}
}
See more Information....

Split string by string

string[] strSplit={"and"};
string[] strOp= TextBox1.Text.Split(strSplit,StringSplitOptions.RemoveEmptyEntries);
for (int iCount=0; iCount < strOp.Length; iCount++)
{
Response.Write("\n");
Response.Write(strOp[iCount].ToString());
Response.Write("\n");
}

Differences between C# Generics and C++ templates:

The following are the key differences between C# Generics and C++ templates:
1. C# generics do not provide the same amount of flexibility as C++ templates. For example, it is not possible to call arithmetic operators in a C# generic class, although it is possible to call user defined operators.
2. C# does not allow non-type template parameters, such as template C {}.
3. C# does not support explicit specialization; that is, a custom implementation of a template for a specific type.
4. C# does not support partial specialization: a custom implementation for a subset of the type arguments.
5. C# does not allow the type parameter to be used as the base class for the generic type.
6. C# does not allow type parameters to have default types.
7. In C#, a generic type parameter cannot itself be a generic, although constructed types can be used as generics. C++ does allow template parameters.
8. C++ allows code that might not be valid for all type parameters in the template, which is then checked for the specific type used as the type parameter. C# requires code in a class to be written in such a way that it will work with any type that satisfies the constraints. For example, in C++ it is possible to write a function that uses the arithmetic operators + and - on objects of the type parameter, which will produce an error at the time of instantiation of the template with a type that does not support these operators. C# disallows this; the only language constructs allowed are those that can be deduced from the constraints.
more.

Boxing & Unboxing.

Converting a value type to reference type is called Boxing.
value type---->Reference
Unboxing is an explicit operation.

class Test
{
static void Main()
{
int i = 1;
object o = i; // boxing(convert value type to reference type)
int j = (int) o; // unboxing(Convert Reference to value type)
}
}