Thursday, August 25, 2011

Get DLL Out of The GAC

DLLs once deployed in GAC (normally located at c:\windows\assembly) can’t be viewed or used as a normal DLL file. They can’t be directly referenced from VS project. Developers usually keep a copy of the original DLL file and refer to it in the project at development (design) time, which uses the assembly from GAC during run-time of the project.

During execution (run-time) if the assembly is found to be signed and deployed in GAC the CLR automatically picks up the assembly from the GAC instead of the DLL referenced during design time in VS. In case the developer has deleted the original DLL or don't have it for some reason, there is a way to get the DLL file from GAC. Follow the following steps to copy DLL from GAC
  1. Run regsvr32 /u C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\yourdllname.dll
  2. yourdllname.ddl is an explorer extension DLL that gives a distinct look to the GAC folder. Unregistering this file will remove the assembly cache viewer and the GAC folder will be then visible as any normal folder in explorer.
  3. Open “%windir%\assembly\GAC_MSIL”.
  4. Browse to your DLL folder into the deep to find your DLL.
  5. Copy the DLL somewhere on your hard disk and refer it from there in your project
  6. Run "regsvr32 /i %windir%\Microsoft.NET\Framework\<.NET version directory> \yourdllname.dll" to re-register the shfusion.dll file and regain the original distinct view of the GAC.

Thursday, August 11, 2011

Add / Upload document to Document Library using SharePoint Object Model

Below is the code to upload document to SharePoint Document Library with all the metadata you wanted to update.


String fileToUpload = @"C:\FileNamePath.txt";
String sharePointSite = "http://urSharePointsite.com/sites/";
String documentLibraryName = "Shared Documents";

using
(SPSite oSite = new SPSite(sharePointSite))
{
   using
(SPWeb oWeb = oSite.OpenWeb())
   {
        SPFolder myLibrary = oWeb.Folders[documentLibraryName];

        //upload document
        String fileName = System.IO.Path.GetFileName(fileToUpload);
        FileStream fileStream = File.OpenRead(fileToUpload);

        // Upload document
        SPFile spfile = myLibrary.Files.Add(fileName, fileStream, true);

        SPListItem spLibItem = spfile.Item;


        spLibItem["Title"] = "Document Title";
        spLibItem["Field2"] = "Field2 Value";
        spLibItem.Update();
     }
}




Fetch user details based on user Alias/ Display Name and Update it to People or Group Field in List


The UserID relates to the ID of the user within the site collection. This is fine but the chances are that not all users are members of the site collection. To address this, the SharePoint OM has a handy little method called SPWeb.EnsureUser() which accepts a Login Name of a user as a string and first checks if the user exists in the site collection, if it does not then it adds the user and returns an SPUser object. Please be aware that you may need to set SPWeb.AllowUnsafeUpdates = True for this to work.
From the SPUser object it is very simple to get the ID (SPUser.ID.ToString() and SPUser.LoginName.ToString()) and then use simple string building to get the string in the right format for the ‘Person or Group’ field.

string sAllContacts = "";

using (SPSite site = new SPSite(“http://sites/site/yoursite”))
{
    site.AllowUnsafeUpdates = true;
    using (SPWeb web = site.RootWeb)
    {
        web.AllowUnsafeUpdates = true;
        string[] aAliases = sAliases.Split(';');
        foreach (string sAlias in aAliases)
        {
            SPUser user = web.EnsureUser(sAlias);
            sAllContacts += user.ID.ToString() + ";#" + user.LoginName.ToString() + ";#";
        }
        web.Update();
    }
}

Wednesday, August 3, 2011

Define managed paths (SharePoint Server 2010)

Published: May 12, 2010
By defining managed paths, you can specify which paths in the URL namespace of a Web application are used for site collections. You can specify that one or more site collections exists at a specified path. This can be a convenient method of consolidating multiple site access for users in various departments.
An explicitly named path (for example, http://server/sites/team) is used for a single site collection. A wildcard path of "sites" (for example, http://server/sites/) indicates that child URLs of the path are site collections.
In this article: