Wednesday, August 22, 2012

Update Windows Local Account Password

I would like to enable users to update their account passwords through a Web Part in SharePoint 2010.

using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;

string userPath = "WinNT://computerName/userName";
DirectoryEntry userEntry = new DirectoryEntry(userPath);
object[] password = {newPassword};
object ret = userEntry.Invoke("SetPassword", password);
userEntry.CommitChanges();

userEntry.Close();

Above snippet code can really update their passwords!

Change local administrator password in C# 

Update Modified / Created in List Item

I would like to migrate a site to another SharePoint Server as a site collection. Unfortunately, I cannot export the site using "Save site as template" (many issues needed to be solved, such as MaxTemplateDocumentSize). 

Apart from "Save site as template", I can choose between site collection backup and export site. For site collection, it includes fields Modified By, Created By, Created and Modified which are absent in site (*.cmp) file. In fact, I cannot move a site up one level without importing site file(*.cmp). Here is a method to update those fields programatically:

            SPListItem item = workflowProperties.Item;
            SPUser user = workflowProperties.Web.AllUsers[@"Test\wawa"];
            SPFieldUserValue currentUser = new SPFieldUserValue(workflowProperties.Web, user.ID, user.LoginName);
            item["Author"] = currentUser;
            item["Editor"] = currentUser;
            item.UpdateOverwriteVersion();

Monday, August 6, 2012

Custom DLL in SharePoint Solution 2010

I reference a custom DLL to a SharePoint Event Receiver solution. But, the event is not triggered when adding a list item. In the log file, an error log can be found:

Could not load type 'SAFP.SharedMethods' from assembly 'SharedMethods, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5e96847a5abc7fb4'

As I have deployed the DLL to GAC already and included the assembly in web.config SafeControl, I have no idea what am I missing.

After asking my friend "Google", it tells me that something should be done on package inside SharePoint Project.

1. Click Package and then click Advanced button
2. Add additonal assmeblies

 
Then I upgrade the solution and everything works fine.