Showing posts with label Sharepoint 2010. Show all posts
Showing posts with label Sharepoint 2010. Show all posts

May 24, 2012

sharepoint 2010 user name not updating after AD display name changed

sharepoint 2010 user name not updating after AD display name changed Try using following step to solve it.: 1. Open "Sharepoint 2010 Management Shell" 2. Set-SPUser -identity "[domain]\[useraccount]” -SyncFromAD -web http://sharepointwebsite

Apr 29, 2011

Sharepoint list items detail about Author,Editor , assignto with mutiple values , Duedate

c# , get sharepoint list item detail about Author,Editor , assignto with mutiple values , Duedate
// Add reference Microsoft.SharePoint.Client and Microsoft.SharePoint.Client.Runtime
//
using Microsoft.SharePoint.Client;


ClientContext clientContext = new ClientContext("http://sharepointserver");
List list = clientContext.Web.Lists.GetByTitle("testlist");
clientContext.Load(list);
clientContext.ExecuteQuery();
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "";
ListItemCollection listItems = list.GetItems(camlQuery);
clientContext.Load(listItems);
clientContext.ExecuteQuery();
int i = 0;
foreach (ListItem listItem in listItems)
{
var SendToList = string.Empty;
var MailSubJect = string.Empty;
var MailBody = string.Empty;

DateTime dt = Convert.ToDateTime(Convert.ToDateTime(listItem["DueDate"]).ToLocalTime().ToString("yyyy/MM/dd"));
DateTime today = Convert.ToDateTime(DateTime.Now.ToLocalTime().ToString("yyyy/MM/dd"));
if ((dt <= today) && (listItem["Status"].ToString().Equals("Finished") == false))
{
Console.WriteLine("Id: {0} Title: {1} Author : {2} Editor : {3}",
listItem.Id, listItem["Title"],
((FieldUserValue)(listItems[i]["Author"])).LookupValue,
((FieldUserValue)(listItems[i]["Editor"])).LookupValue
);
String StartDateStr = Convert.ToDateTime(listItem["StartDate"]).ToLocalTime().ToString("yyyy/MM/dd");
String DueDateStr = Convert.ToDateTime(listItem["DueDate"]).ToLocalTime().ToString("yyyy/MM/dd");
String ModifiedStr = Convert.ToDateTime(listItem["Modified"]).ToLocalTime().ToString("yyyy/MM/dd HH:mm:ss");
double PercentComplete = Convert.ToDouble(listItem["PercentComplete"].ToString());
PercentComplete = PercentComplete * 100;
String PercentCompleteStr = PercentComplete.ToString();
var BodyStr = string.Empty;
if (listItem["Body"]!=null)
BodyStr = listItem["Body"].ToString();
var assigntolist = string.Empty;
Type t = listItems[i]["AssignedTo"].GetType();
if (t.IsArray)
{
foreach (FieldUserValue o in listItems[i]["AssignedTo"] as FieldUserValue[])
Console.WriteLine("AssignedTo: {0}", o.LookupValue);
}
}
i = i + 1;
}

C# send mail by smtp with html body

C# send mail by smtp with html body. (add reference System.Net)
using System.Net.Mail;

public static void Sendalertmail(string SendToList, string MailSubJect, string MailBody)
{

string fromEmail = "Administrator@test.com";//sending email from...
string ToEmail = SendToList; //destination email
string body = MailBody;
string subject = MailSubJect;
try
{
MailMessage mm = new MailMessage(fromEmail, ToEmail);
mm.Subject = MailSubJect;
mm.Body = MailBody;
mm.IsBodyHtml = true;
SmtpClient sMail = new SmtpClient("192.168.1.1");//exchange or smtp server goes here.
sMail.DeliveryMethod = SmtpDeliveryMethod.Network;
sMail.Send(mm);
}
catch (Exception ex)
{
//do something
}

}

Get sharepoint user name or email address

c# get sharepoint user name or email address
-------
static public string GetUserEMail(object user, ClientContext context)
{
Dictionary userNameCache = new Dictionary();
if (user == null)
{
return string.Empty;
}

var username = string.Empty;
var useremail = string.Empty;
var spUser = user as FieldUserValue;
if (spUser != null)
{
if (!userNameCache.TryGetValue(spUser.LookupId, out useremail))
{
var userInfoList = context.Web.SiteUserInfoList;
context.Load(userInfoList);
var query = new CamlQuery { ViewXml = "" + spUser.LookupId + "" };
var users = userInfoList.GetItems(query);
context.Load(users, items => items.Include(
item => item.Id,
item => item["Name"]));
var principal = users.GetById(spUser.LookupId);
context.Load(principal);
context.ExecuteQuery();
username = principal["Name"] as string;
useremail = username.Substring(username.IndexOf("\\") + 1) + "@test.com";
userNameCache.Add(spUser.LookupId, useremail);
}
}
return useremail;
}

Jan 7, 2011

Sharepoint 2010 disable user "alert me"

There are two ways to disable "alert me"
1. Sharepoint administration -> application management -> Web Application -> Manage web application -> Application Ex. 80 , then "general Settings"
-> set "Alerts" option as "off"

2. Through permission level , remove "alert me".

Sharepoint 2010 master page layout , fix content display width .

1.
## Copy v4.master as custom.master , and set as default master page , and then under <head> , above </head> add following code -> style type="text/css" ...

<style type="text/css">
.s4-specialNavLinkList
{
display:none !important;
}
.ms-cui-ribbonTopBars > DIV {
MARGIN: 0px auto; WIDTH: 1000px; FLOAT: none
}
UL.ms-cui-tabBody {
MARGIN: 0px auto; WIDTH: 1000px; FLOAT: none
}
BODY #s4-titlerow > DIV {
MARGIN: 0px auto; WIDTH: 1000px; FLOAT: none
}
#s4-bodyContainer > DIV {
MARGIN: 0px auto; WIDTH: 1000px; FLOAT: none
}
.s4-help {
DISPLAY: none !important;
}
.s4-search {
DISPLAY: none !important;
}
.s4-titletext {
DISPLAY: none !important;
}
body #s4-leftpanel{font-size: 10pt !important; font-family: Verdana !important;}
</style>


2. Hide the left panel at all.
1. Site Actions > Edit Page >
2. # Insert > Web Part (or add a Web Part to a Web Part zone)
3. # Under Media and Content select Content Editor and click Add , then save
4. Enter the following HTML / CSS into content and click OK

<style type="text/css">
body #s4-leftpanel { display: none; }
.s4-ca { margin-left: 0px; }
</style>


## There is a good reference doc for sharepoint 2010 layout component at http://erikswenson.blogspot.com/2010/01/sharepoint-2010-base-css-classes.html

Nov 2, 2010

Sharepoint 2010 connect to sql server and oracle database

1. Menu add connection strings in c:\inetpub\wwwroot\wss\VirtualDirectories\\web.config
Ex.
/* for sql server and oracle*/
<connectionStrings>
<add name="AdventureWorksConnectionString" connectionString="Data Source=192.168.0.1;Initial Catalog=AdventureWorks;User Id=sa;Password=smpsqladm1" />
<add name="OracleConnectionString" connectionString="Data Source=Ora_TEST;User ID=system;Password=system_pwd" providerName="System.Data.OracleClient" />
</connectionStrings>

2. In Sharepoint virtual web part ascx file .

<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px"
DataSourceID="SqlDataSource1" EnableModelValidation="True">
</asp:DetailsView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorksConnectionString%>"
SelectCommand="select count(*) as intUnRead from yourtable">
</asp:SqlDataSource>

<asp:DetailsView ID="DetailsView2" runat="server" Height="50px" Width="125px"
DataSourceID="SqlDataSource2" EnableModelValidation="True">
</asp:DetailsView>

<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:OracleConnectionString %>"
ProviderName="System.Data.OracleClient"
SelectCommand="select count(*) as UnRead from testtable">
</asp:SqlDataSource>

Nov 1, 2010

Using Visual Studio 2010 add connect string to Sharepoint 2010 to query database data

1. From VS2010 , add new sharepoint 2010 empty project as , and then add one new item "Virtual Web Part" name as "SQLWebPartTest"
2. In "SQLWebPartTest" ascx file , manual add codes for DetailsView and SqlDataSource .
-----------
<asp:DetailsView ID="DetailsView1" runat="server"
DataSourceID="SqlDataSource1" EnableModelValidation="True">
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ChangebyYourConnectionString %>"
SelectCommand="select * from ChangebyYourTable">
</asp:SqlDataSource>

<asp:DetailsView ID="DetailsView2" runat="server"
DataSourceID="SqlDataSource2" EnableModelValidation="True">
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ChangebyYourConnectionString2 %>"
SelectCommand="select * from ChangebyYourTable">
</asp:SqlDataSource>
--------------
3. In the project "Solution Explorer" , right click in "Feature1" to add one "Event Receiver" and name it as "Feature1.EventReceiver.cs"
--- In "Feature1.EventReceiver.cs"
#1 Add using
using System.Reflection;
using Microsoft.SharePoint.Administration;

#2 Add following code in "public class Feature1EventReceiver : SPFeatureReceiver"
private ModificationEntry[] entries =

//Ensure there's a connectionStrings section.
new ModificationEntry(
"connectionStrings"
,"configuration"
,"<connectionStrings/>"
,SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode
,true)
//Create the connectionstring.
,new ModificationEntry(
"add[@name='ChangebyYourConnectionString'][@connectionString='Data Source=192.168.0.1;Initial Catalog=ChangebyYourDB;User Id=ChangebyYourID;Password=ChangebyYourPWD']"
,"configuration/connectionStrings"
,"<add name='ChangebyYourConnectionString' connectionString='Data Source=192.168.0.1;Initial Catalog=ChangebyYourDB;User Id=ChangebyYourID;Password=ChangebyYourPWD'/>"
,SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode
,false)
,new ModificationEntry(
"add[@name='ChangebyYourConnectionString2'][@connectionString='Data Source=192.168.0.100;Initial Catalog=AdventureWorks;User Id=ChangebyYourID;Password=ChangebyYourPWD']"
,"configuration/connectionStrings"
,"<add name='ChangebyYourConnectionString2' connectionString='Data Source=192.168.2.100;Initial Catalog=AdventureWorks;User Id=ChangebyYourID;Password=ChangebyYourPWD'/>"
,SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode
,false)
};

public override void FeatureActivated(SPFeatureReceiverProperties properties)

//Get a reference to the web application and then remove entries for the blorum.
SPSite site = properties.Feature.Parent as SPSite;
SPWebApplication webApplication = site.WebApplication;

site.RootWeb.Title = "Set from activating code at " + DateTime.Now.ToString();
site.RootWeb.Update();

foreach (ModificationEntry entry in entries)

webApplication.WebConfigModifications.Add(CreateModification(entry));


webApplication.WebService.WebConfigModifications.Clear();
webApplication.WebService.ApplyWebConfigModifications();




public override void FeatureDeactivating(SPFeatureReceiverProperties properties)

SPSite site = properties.Feature.Parent as SPSite;
SPWebApplication webApplication = site.WebApplication;

site.RootWeb.Title = "Set from deactivating code at " + DateTime.Now.ToString();
site.RootWeb.Update();

foreach (ModificationEntry entry in entries)

if (!entry.CreateOnly)
webApplication.WebConfigModifications.Remove(CreateModification(entry));


webApplication.WebService.ApplyWebConfigModifications();



private SPWebConfigModification CreateModification(ModificationEntry entry)

SPWebConfigModification modification = new SPWebConfigModification(entry.Name, entry.XPath);
modification.Owner = Assembly.GetExecutingAssembly().FullName;
modification.Sequence = 0;
modification.Type = entry.ModificationType;
modification.Value = entry.Value;

return modification;


private struct ModificationEntry

public string Name;
public string XPath;
public string Value;
public SPWebConfigModification.SPWebConfigModificationType ModificationType;
public bool CreateOnly;

public ModificationEntry(string name, string xPath, string value,
SPWebConfigModification.SPWebConfigModificationType modificationType, bool createOnly)

Name = name;
XPath = xPath;
Value = value;
ModificationType = modificationType;
CreateOnly = createOnly;


4. Press F5 in VS2010 to open Sharepoint site , and insert add webpart from "custom" location.