Sep 14th, 2008 | No Comments

Here is the sample code thar you can use to access Active Directory details from a sharepoint site,

DirectoryEntry oDE;
oDE = new DirectoryEntry(“LDAP://<ldapserver>,”ADusername”,”ADpassword”,AuthenticationTypes.Secure);

DirectoryEntry de = oDE;

DirectorySearcher deSearch = new DirectorySearcher();

deSearch.Filter = “(&(objectClass=user)(SAMAccountName=” + SPContext.Current.Web.CurrentUser.LoginName + “))”;

deSearch.SearchScope = SearchScope.Subtree;

SearchResult results = deSearch.FindOne();

if (!(results == null))
{
de = new DirectoryEntry(results.Path,”AD username”,”AD password”,AuthenticationTypes.Secure);

//if you know the propertyname you want to get value from you can use this
de.Properties["propertyname"].ToString();

//this code will loop thru all the properties in active directory
ResultPropertyCollection propertiesCollection;

propertiesCollection = results.Properties;

//loop thru all the properties in AD
foreach (string currentProperty in propertiesCollection.PropertyNames)
{

Response.Write(“Property Name: ” + currentProperty);

//loop thru all the sub properties
foreach (Object thisCollection in propertiesCollection[currentProperty])
{

Response.Write(thisCollection.ToString() + “<br/>”);
}
}
}

The above code reads and writes the values of the properties from the Active directory for the current user.
But to be able to run the above code you need to make sure that you import System.DirectoryServices,
<%@ Import Namespace=”System.DirectoryServices” %>
Also you need to make sure Web.config has an entry for the System.DirectoryServices in its Assemblies Section,

<assemblies>
<add assembly=”System.DirectoryServices, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089″ />
</assemblies>

Written by Ajay Matharu

September 14th, 2008 at 8:06 am

Posted in Sharepoint

Tagged with

Aug 23rd, 2008 | 2 Comments

Today I came across a requirement of showing an swf file in the webpart. So I thought I will upload the swf file in folder and give its url to the webpart. But then that will be hardcoded path. What will happen if the user deleted that folder or rather deleted that file itself? So my webpart will not run.

So I found some alternative to this and thought of embedding my swf file and js file in webpart itself. I googled on how can I do this, and here are the steps for embedding the files in the webpart itself.

I ll assume that you are ready with the Webpart and you just need to remove the hard coded file’s url’s in your webpart. And i ll be using swfobject.js file in this example you can change the file and the type though, as per your requirements.

- Add swfobject.js file in the same project as webpart.

- Right click on js file select properties and change build action to “Embedded Resource”.

- Make an entry in AssemblyInfo.cs or AssemblyInfo.vb file like

[assembly: WebResource("ReadENote.swfobject.js", "text/js")]

format : [assembly: WebResource("<project namespace>.<filename>.<extension>", "<content type>")]

Note : Make sure you specify the namespace along with filename else it will not recognize the file.

- You can access this embedded resource in your webpart code like this

Type t = typeof(<webpartname>); //e.g. Type t = typeof(ReadENote);
string js = “<namespace>.<filename>.<extension>”; //e.g. string js = “ReadENote.swfobject.js”;
jslocation = Page.ClientScript.GetWebResourceUrl(t, js);
Page.ClientScript.RegisterClientScriptResource(t, js);

This code will embed the specified file, in this case swfobject.js file in my webpart.

Note: you only need to use the last line “Page.ClientScript.RegisterClientScriptResource(t, js);” if you are using the javascript file. If you are using some other file like an image file or swf file, you can use the jslocation in place of your hard coded location.

- my previous code to embed swf file looks like this

strBody = strBody + “var so = new SWFObject(\”notes.swf\”, \”stickies\”, \”250\”, \”250\”, \”9\”, \”#FF6600\”);”;

- my code after using these steps will look like this

Type t = typeof(<webpartname>);

string js = “<namespace>.<filename>.<extension>”;

jslocation = Page.ClientScript.GetWebResourceUrl(t, js);

strBody = strBody + “var so = new SWFObject(\”" + swflocation + “\”, \”stickies\”, \”250\”, \”250\”, \”9\”, \”#FF6600\”);”;

Make these changes, build and deploy your web part.

Enjoy dynamic coding and prevent hard code.

Written by Ajay Matharu

August 23rd, 2008 at 2:02 pm

Posted in Sharepoint

Page 6 of 6« First23456