Null values gathering data for Aspose Dynamic Slide Generation CRM 2011 - c#

I am having with using Aspose to generate dynamic content in slides within Microsoft CRM Dynamics 2011. The data is not populating correctly for several fields and relationals even though the logic in the code seems to be correct and should not yield a null or empty value, and I have confirmed there is indeed data for the fields in the database (The data does populate correctly for some of the fields and relationals). All of these issues occur before the powerpoint itself even starts to be generated, so in reality the issues may not be “Aspose” issues they may just be Web Service / CRM issues with retrieving the data asynchronously.
Aspose Slide Workflow:
1) An HTML web resource includes a Silverlight button to download powerpoint report
2) On button click the Silverlight application Asyncronosously collects all the data needed from the Project entity in CRM through web service requests
3) The data is compiled into a Project class that is then passed over to the ReportGenerator Service and uses the data to populate the parts of the powerpoint (This is the part where we use the custom Aspose stuff)
4) Aspose functionality goes through each specified slide and parses them adding in the data where required.
5) Powerpoint is completed and spit out for the user with the data populated.
These are two examples of where I am having troubles getting data to populate. The project.Pwins value below ends up being null which tells me the lambda expression is not executing appropriately, it remains null at the end of the method which tells me the highlighted line is not even being executed at all.
Example 1:
/// <summary>
/// Retrieves and stores information required for P(win) report, and then tries to send a web service request.
/// </summary>
/// <param name="service"></param>
private void LoadPwin(ReportServiceClient service)
{
string filter = string.Format("?$filter=sfa_Project/Id eq guid'{0}'", GetProjectId());
string url = GetEntitySetAddress("sfa_pwin_competitor") + filter;
WebClient client = new WebClient();
client.DownloadStringCompleted += (sender, args) =>
{
if (args.Error == null)
{
StringReader stream = new StringReader(args.Result);
XmlReader reader = XmlReader.Create(stream);
List<PWin> pwins = new List<PWin>();
List<Dictionary<string, string>> dictionaries = LoadEntitiesFromXml(reader);
foreach (Dictionary<string, string> dictionary in dictionaries)
{
PWin pwin = new PWin();
pwin.CompanyName = ParseDictionaryValue(dictionary["sfa_competitor"]);
pwin.IsBoeing = (ParseDictionaryValue(dictionary["sfa_is_boeing"]) == "true");
pwin.IsDomestic = (ParseDictionaryValue(dictionary["sfa_domestic_or_international"]) == "true");
pwin.AffordabilityWeight = ParseDictionaryValueToNumber(dictionary["sfa_affordability_weight"]);
pwin.AffordabilityScore = ParseDictionaryValueToNumber(dictionary["sfa_affordability_score"]);
pwin.CustomerRelationshipWeight = ParseDictionaryValueToNumber(dictionary["sfa_customer_relationship_weight"]);
pwin.CustomerRelationshipScore = ParseDictionaryValueToNumber(dictionary["sfa_customer_relationship_score"]);
pwin.CustomerAdvantageWeight = ParseDictionaryValueToNumber(dictionary["sfa_customer_advantage_weight"]);
pwin.CustomerAdvantageScore = ParseDictionaryValueToNumber(dictionary["sfa_customer_advantage_score"]);
pwin.CompetitionWeight = ParseDictionaryValueToNumber(dictionary["sfa_competition_weight"]);
pwin.CompetitionScore = ParseDictionaryValueToNumber(dictionary["sfa_competition_score"]);
pwin.CPOBCWeight = ParseDictionaryValueToNumber(dictionary["sfa_cpobc_weight"]);
pwin.CPOBCScore = ParseDictionaryValueToNumber(dictionary["sfa_cpobc_score"]);
pwin.CompanyResourcesWeight = ParseDictionaryValueToNumber(dictionary["sfa_company_resources_weight"]);
pwin.CompanyResourcesScore = ParseDictionaryValueToNumber(dictionary["sfa_company_resources_score"]);
pwin.CompanyResourcesInvestmentWeight = ParseDictionaryValueToNumber(dictionary["sfa_company_resources_investment_weight"]);
pwin.CompanyResourcesInvestmentScore = ParseDictionaryValueToNumber(dictionary["sfa_company_resources_investment_score"]);
pwin.ProgramBackgroundWeight = ParseDictionaryValueToNumber(dictionary["sfa_program_background_weight"]);
pwin.ProgramBackgroundScore = ParseDictionaryValueToNumber(dictionary["sfa_program_background_score"]);
pwin.ContinuityOfEffortWeight = ParseDictionaryValueToNumber(dictionary["sfa_continuity_of_effort_weight"]);
pwin.ContinuityOfEffortScore = ParseDictionaryValueToNumber(dictionary["sfa_continuity_of_effort_score"]);
pwin.ExecutionWeight = ParseDictionaryValueToNumber(dictionary["sfa_execution_weight"]);
pwin.ExecutionScore = ParseDictionaryValueToNumber(dictionary["sfa_execution_score"]);
pwin.TechnicalSolutionWeight = ParseDictionaryValueToNumber(dictionary["sfa_technical_solution_weight"]);
pwin.TechnicalSolutionScore = ParseDictionaryValueToNumber(dictionary["sfa_technical_solution_score"]);
pwin.StrategyToWinWeight = ParseDictionaryValueToNumber(dictionary["sfa_strategy_to_win_weight"]);
pwin.StrategyToWinScore = ParseDictionaryValueToNumber(dictionary["sfa_strategy_to_win_score"]);
pwin.ManagementStrengthWeight = ParseDictionaryValueToNumber(dictionary["sfa_management_strength_weight"]);
pwin.ManagementStrengthScore = ParseDictionaryValueToNumber(dictionary["sfa_management_strength_score"]);
pwin.CustomerPercievedCommitmentWeight = ParseDictionaryValueToNumber(dictionary["sfa_customers_percieved_commitment_weight"]);
pwin.CustomerPercievedCommitmentScore = ParseDictionaryValueToNumber(dictionary["sfa_customers_percieved_commitment_score"]);
pwin.PastPerformanceWeight = ParseDictionaryValueToNumber(dictionary["sfa_past_performance_weight"]);
pwin.PastPerformanceScore = ParseDictionaryValueToNumber(dictionary["sfa_past_performance_score"]);
pwin.RawPWin = ParseDictionaryValueToDecimal(dictionary["sfa_pwin_score"]);
pwin.RelativePWin = ParseDictionaryValueToDecimal(dictionary["sfa_relative_pwin_score"]);
pwins.Add(pwin);
}
project.PWins = new ObservableCollection<PWin>(pwins);
PwinReady = true;
reader.Close();
TrySendRequest(service);
}
};
client.DownloadStringAsync(new Uri(url));
}
Example 2 the project.TeamMembers value below ends up being null:
/// <summary>
/// Retrieves and stores information required for Capture Team Roster report, and then tries to send a web service request.
/// </summary>
/// <param name="service"></param>
private void LoadCaptureTeamRoster(ReportServiceClient service)
{
string filter = string.Format("?$select=sfa_name,sfa_Role&$filter=sfa_Project/Id eq guid'{0}'", GetProjectId());
string url = GetEntitySetAddress("sfa_team_roster") + filter;
WebClient client = new WebClient();
client.DownloadStringCompleted += (sender, args) =>
{
if (args.Error == null)
{
StringReader stream = new StringReader(args.Result);
XmlReader reader = XmlReader.Create(stream);
List<TeamMember> members = new List<TeamMember>();
List<Dictionary<string, string>> dictionaries = LoadEntitiesFromXml(reader);
foreach (Dictionary<string, string> dictionary in dictionaries)
{
TeamMember member = new TeamMember();
member.Name = ParseDictionaryValue(dictionary["sfa_name"]);
member.Role = ParseDictionaryValue(dictionary["sfa_role"]);
members.Add(member);
}
project.TeamMembers = new ObservableCollection<TeamMember>(members);
CaptureTeamRosterReady = true;
reader.Close();
TrySendRequest(service);
}
};
client.DownloadStringAsync(new Uri(url));
}
Here is another example that is not an issue with a relational, but instead is an issue with a field populated on a project entity in CRM that shows up empty after retrieving the data. Both CaptureTeamLeader and ProgramManager end up being empty strings, however the ProjectName and ProjectNumber have no troubles populating
private void LoadCampaignTitle(ReportServiceClient service)
{
project.ProjectName = GetAttributeValue("sfa_name");
project.ProjectNumber = GetAttributeValue("sfa_project_number");
project.CaptureTeamLeader = GetAttributeValue("sfa_capture_team_leader_emp");
project.ProgramManager = GetAttributeValue("sfa_program_manager_emp");
CampaignTitleReady = true;
TrySendRequest(service);
}
Any help would be greatly appreciated. Thanks in advance!
EDIT:
This is the AttributeValue method asked for:
/// <summary>
/// Gets the value of provided attribute from the current page's Xrm data. If for any reason the retrieval fails, returns an empty string.
/// </summary>
/// <param name="attributeName"></param>
/// <returns></returns>
private string GetAttributeValue(string attributeName)
{
// NOTE: This is the preferred way to retrieve data from the CRM. However for some unknown issue,
// this always returns NULL. It will be replaced with directly calling .eval to the window object.
// dynamic Xrm = (ScriptObject)HtmlPage.Window.GetProperty("window.parent.Xrm");
try
{
return HtmlPage.Window.Invoke("getAttributeValue", attributeName).ToString();
}
catch (ArgumentNullException)
{
return string.Empty;
}
catch (ArgumentException)
{
return string.Empty;
}
catch (InvalidOperationException)
{
return string.Empty;
}
}

The solution to this problem was that number 1 The downloadstringasync and other places in my code were doing things asynchronously and therefore the debugger was throwing me off and showing things as null when they were really just being executed later. The actual fix required some changes to a client.xml file in the folder the webservice was being hosted from.

Related

Why is my object created on backend C# becoming null

I'm using asp.net and c# for backend code and html, javascript/jquery on frontend
User clicks "Details" on website to view and edit requests previously created.
jquery/ajax calls method on backend to get infomation about requests.
An object, newWaferRequest, is created which holds this information. The object is returned from ajax call and the site populates a form with that information. The user can then edit the form, add new programs to requests, modify existing requests info etc...
The problem I'm getting is when the user selects "Save pgm", ajax calls backend savePgm method which is supposed to update the Object newWaferRequest previously created, but I'm receiving a nullReferenceException. The exception is pointing to the newWaferRequest object being null. This happens at different times when the user is updating the request. Sometimes the user can update the program and add new programs, with a correct response from backend. Other times the exception occurs.
I must not have a full grasp of how variables are "stored" when using the web. I read multiple post on here and have read about Session and ViewState variables. I've tried saving the Object in a Session with no luck.
This is just showing the beginning of my c# code, I tried using static variables initially. All the objects are in App_code cs files.
public partial class Nbump_Request : System.Web.UI.Page
{
//public static person currentPerson = new person();
//public static requestSearch searchRequest;
//public static waferRequest newWaferRequest;
//public static waferRequestObject newWaferRequestObject;
static person currentPerson = new person();
static requestSearch searchRequest;
static waferRequest newWaferRequest;
static waferRequestObject newWaferRequestObject;
Here is ajax method for existing requests, which creates objects
[WebMethod]
public static string queryRequestExisting(string request_pk)
{
JavaScriptSerializer js = new JavaScriptSerializer();
try
{
newWaferRequest = new waferRequest(request_pk, "EXISTINGREQUEST");
newWaferRequestObject = newWaferRequest.CURRENTREQUEST;
string json = js.Serialize(newWaferRequest);
return json;
}
catch (Exception)
{
return null;
} 
}
Here is ajax code for when user saves or updates program information. I added in the code to check and return with the object was null.
public static string savePgmData(Dictionary<string, string> pgm_requests)
{
PgmRequestObject currentPgm;
if (newWaferRequest != null)
{
try
{
//if (!Convert.ToBoolean(pgm_requests["pgmEdit"])) //not performed when request pgm being edited
string rtnMessage;
//create new pgm in waferRequest object
if (pgm_requests["PGMRT_INDEX_PK"] == null)
{
int index = newWaferRequest.addNewPGMRequest() - 1;
currentPgm = (PgmRequestObject)newWaferRequest.PGMREQUESTLIST[index];
string pgmPrimaryKey = currentPgm.PGMRT_INDEX_PK;
rtnMessage = "{\"message\": \"Added and saved new pgm successfully\", \"primaryKey\":\"" + pgmPrimaryKey + "\"}";
}
//get pgm to update at index provided
else if (pgm_requests["pgmRowIndex"] != "-1")
{
int index = Convert.ToInt32(pgm_requests["pgmRowIndex"]);
currentPgm = (PgmRequestObject)newWaferRequest.PGMREQUESTLIST[index];
rtnMessage = "Edited and saved pgm successfully";
}
//save initial pgm
else
{
currentPgm = (PgmRequestObject)newWaferRequest.PGMREQUESTLIST[0];
rtnMessage = "Initial pgm added successfully";
}
 
 
currentPgm.updatePGM(pgm_requests["pgmNameList"], pgm_requests["pgmNameText"]);
currentPgm.updateStatus(pgm_requests["pgmStatus"]);
currentPgm.updateWafers(pgm_requests["pgmWfrList"], pgm_requests["pgmWfrNum"]);
currentPgm.updatePriority(pgm_requests["pgmTestOrder"]);
currentPgm.updateFailBinRetest(pgm_requests["pgmFbr"], pgm_requests["pgmFbrBins"]);
//currentPgm.CommitIntoDatabase(); // -- Called on individual requests updates before request saved
 
return rtnMessage;
}
catch (Exception ex)
{
return "Failed to save program\n\n" + ex.Data + "\n" + ex.Message + "\n" + ex.StackTrace + "\n";
}
}
else
{
return "newWaferRequest object is null\n";
}
}
This is a short snippet of the class for waferRequest. This is located in the app_code and is a cs file. The pgmRequestList contains an array of pgm objects. That is what I update when user selects "Save Program".
public class waferRequest
{
/*The class will hold request information, creation and delete.
*
*/
private waferRequestObject currentRequest;
private ArrayList WaferRequestList = new ArrayList();
private ArrayList PGMRequestList = new ArrayList();
Here is an example of what I tried for Session. I got rid of static variables and modified the methods
[WebMethod(EnableSession=true)]
public static string queryRequestExisting(string request_pk)
{
waferRequest newWaferRequest;
waferRequestObject newWaferRequestObject;
JavaScriptSerializer js = new JavaScriptSerializer();
try
{
newWaferRequest = new waferRequest(request_pk, "EXISTINGREQUEST");
newWaferRequestObject = newWaferRequest.CURRENTREQUEST;
HttpContext.Current.Session["newWaferRequest"] = newWaferRequest;
HttpContext.Current.Session["newWaferRequestObject"] = newWaferRequestObject;
string json = js.Serialize(newWaferRequest);
return json;
}
catch (Exception)
{
return null;
} 
}
//snippet of savePgmData with session
public static string savePgmData(Dictionary<string, string> pgm_requests)
{
waferRequest newWaferRequest = HttpContext.Current.Session["newWaferRequest"];
PgmRequestObject currentPgm;
if (newWaferRequest != null)
{
try
{
//if (!Convert.ToBoolean(pgm_requests["pgmEdit"])) //not performed when request pgm being edited
string rtnMessage;
//create new pgm in waferRequest object
if (pgm_requests["PGMRT_INDEX_PK"] == null)
{
int index = newWaferRequest.addNewPGMRequest() - 1;
currentPgm = (PgmRequestObject)newWaferRequest.PGMREQUESTLIST[index];
I am limited to the code I'm allowed to show because of work security. Please let me know if more information is needed.
Also, the code seems to work fine on my local server. The issue is when I put it on the production server or development server -- Both of which I don't have access to make changes on.

c# Entity Framework EF 4.1 Change Schema and Database name at runtime

I´ve searched for some topics, but did not find a concrete solution to my problem.
My app is a c# commercial app. I´m using EF 4.1 database first. I generate the model from a development database connection and that create a Model.edmx file and all EF stuff pretty fine.
My goal is to deliver an appplication to the customer and let him freely create the database and database user at his own. Do do it, at runtime I would get the username, password, database connection and schema name parameters to connect to customer database. In that way, to deploy the application all I need is to ask the customer to create a database and add the database parameters to the app config file.
So, myy goal is to change the connection string and schema parameter at runtime, without changing att all the auto generated edmx file, not touching the VS generated code.
I have looked around and found:
For EF earlier versions:
Changing schema name on runtime - Entity Framework
http://efmodeladapter.codeplex.com
All other posts are going around that. I tryed even to use the first post code with no success.
But I´ve seen that EF 4.1 comes with better support tools to do it, but I could not find references or examples to it. It´s important not to change the auto generated code from VS.
I´m pretty new to EF, so I would like to ask for help on accomplish the following tasks:
a) Change connection string at runtime adding my username, password and database server/port parameters
b) Change database schema
I´m using Oracle as a database server (that makes things worst as Oracle mixes schema and users together).
Actually, I needed solution for this too. I quickly whipped up solution, which works nicely. I didn't find much information about this in Internet, so I am not sure about the "EF 4.1 comes with better support tools to do it".
The concrete example "Changing schema name on runtime - Entity framework" didn't entirely work for me, however with some minor modifications I got it working.
Here is a DatabaseUtils class that can do it:
internal static class DatabaseUtils
{
/// <summary>
/// Builds the connection string for Entity framework.
/// </summary>
/// <returns></returns>
public static EntityConnection BuildConnection(BuildConnectionParams buildConnectionParams)
{
var sqlBuilder = new SqlConnectionStringBuilder
{
DataSource = buildConnectionParams.ServerName,
InitialCatalog = buildConnectionParams.DatabaseName,
IntegratedSecurity = true
};
var providerString = sqlBuilder.ToString();
var entityBuilder = new EntityConnectionStringBuilder
{
Provider = buildConnectionParams.ProviderName,
ProviderConnectionString = providerString,
Metadata = string.Format(#"res://*/{0}.csdl|
res://*/{0}.ssdl|
res://*/{0}.msl", buildConnectionParams.ModelName)
};
return CreateConnection(buildConnectionParams.SchemaName, entityBuilder, buildConnectionParams.ModelName);
}
/// <summary>
/// Creates the EntityConnection, based on new schema & existing connectionString
/// </summary>
/// <param name="schemaName">Name of the schema.</param>
/// <param name="connectionBuilder"></param>
/// <param name="modelName">Name of the model.</param>
/// <returns></returns>
private static EntityConnection CreateConnection(string schemaName, EntityConnectionStringBuilder connectionBuilder, string modelName)
{
Func<string, Stream> generateStream =
extension => Assembly.GetExecutingAssembly().GetManifestResourceStream(string.Concat(modelName, extension));
Action<IEnumerable<Stream>> disposeCollection = streams =>
{
if (streams == null)
return;
foreach (var stream in streams.Where(stream => stream != null))
stream.Dispose();
};
var conceptualReader = generateStream(".csdl");
var mappingReader = generateStream(".msl");
var storageReader = generateStream(".ssdl");
if (conceptualReader == null || mappingReader == null || storageReader == null)
{
disposeCollection(new[] { conceptualReader, mappingReader, storageReader });
return null;
}
var storageXml = XElement.Load(storageReader);
foreach (var entitySet in storageXml.Descendants())
{
var schemaAttribute = entitySet.Attributes("Schema").FirstOrDefault();
if (schemaAttribute != null)
schemaAttribute.SetValue(schemaName);
}
storageXml.CreateReader();
var workspace = new MetadataWorkspace();
var storageCollection = new StoreItemCollection(new[] { storageXml.CreateReader() });
var conceptualCollection = new EdmItemCollection(new[] { XmlReader.Create(conceptualReader) });
var mappingCollection = new StorageMappingItemCollection(conceptualCollection,
storageCollection,
new[] { XmlReader.Create(mappingReader) });
workspace.RegisterItemCollection(conceptualCollection);
workspace.RegisterItemCollection(storageCollection);
workspace.RegisterItemCollection(mappingCollection);
var connection = DbProviderFactories.GetFactory(connectionBuilder.Provider).CreateConnection();
if (connection == null)
{
disposeCollection(new[] { conceptualReader, mappingReader, storageReader });
return null;
}
connection.ConnectionString = connectionBuilder.ProviderConnectionString;
return new EntityConnection(workspace, connection);
}
}
Usage:
/// <summary>
/// Initializes a new instance of the <see cref="DynamicAQDContext"/> class.
/// </summary>
public DynamicAQDContext()
{
var entityConnection = DatabaseUtils.BuildConnection(new BuildConnectionParams
{
ProviderName = "System.Data.SqlClient",
ServerName = "localhost\\",
DatabaseName = "",
ModelName = "YOURMODEL",
SchemaName = "SCHEMA"
});
if(entityConnection == null)
throw new Exception("Can't create EntityConnection");
_entities = new LINKEntities(entityConnection);
}
more info can be found here: http://bestplayah.com/entity-framework-dynamic-schema-changes-using-database-first-approach/

Changing title of documents being uploaded - Event Receiver

Ok so I have a request from a user and I want to achieve it using and Event Receiver.
Basically what she wants is to have a Document Library where users can upload documents. Each document will have a file name like this "UserA - Customization.docx". Now you can imagine that a user can upload many documents that will have an identical name so what we want to do is number the files automatically. So if if UserA uploads the first document, SharePoint will add a number after the name so the file will be called "UserA - Customization - 1.docx" and then he uploads a second document and it will be called "UserA - Customization - 2.docx".
However, now if UserB wants to upload his first document it must be numbered "UserB - Customization - 1.docx" so basically the counter needs to restart if its a new documet and continue from the highest number if the document name already exists.
So basically SharePoint needs to check if the name of the current document exists in the list and if it does add a number next to it, but the number must be 1 greater than the highest document so it will just increase.
Is there any way to do this? Any ideas?
This is what I have come up with so far for simply changing the name of the file adding "-xx" to the filename but this is not working.
public override void ItemAdded(SPItemEventProperties properties)
{
SPFile spf = properties.ListItem.File;
string url = properties.AfterUrl;
int positionOfSlash = url.LastIndexOf("/");
string pathBeforeFileName = url.Substring(0, positionOfSlash);
string newFileName = createNewFileName(url.Substring(positionOfSlash));
string myNewUrl = pathBeforeFileName + newFileName;
DisableEventFiring();
spf.MoveTo(myNewUrl);
spf.Update();
EnableEventFiring();
}
static string createNewFileName(string oldFileName)
{
int positionOfPeriod = oldFileName.LastIndexOf(".");
string fileName = oldFileName.Substring(0, positionOfPeriod);
string fileExtension = oldFileName.Substring(positionOfPeriod);
string newFileName = fileName + "-xx" + fileExtension;
return newFileName;
}
Where am I going wrong with this code? Thanks for any help!
EDIT: this is the code I am using in the console app in Visual Studio to attach the EventReceiver to the Document Library.
using (SPSite site = new SPSite("http://servername:port/subsite1/subsite2/"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["My Doc Library"];
SPEventReceiverDefinition def = list.EventReceivers.Add();
def.Assembly = "DocumentLibrary_ClassLib, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=611205b34d18f14d";
def.Class = "DocumentLibrary_ClassLib.EventReceiver";
def.Type = SPEventReceiverType.ItemAdded;
def.Update();
}
}
EDIT #2:
Ok how about something like this?
//this will get just the name of the file without the extension and I will send that to the
//query builder which will count how many files there are with that name and return
int positionOfPeriod = oldFileName.LastIndexOf(".");
string tempFileName = oldFileName.Substring(0, positionOfPeriod);
SPQuery query = BuildArbitraryQuery(properties.List, "Name", tempFileName, true);
But now I don't really understand the query in the BuildArbitraryQuery, how can I change this to give me the desired behavior? (sorry if its a total noob question but I've never dealt with C# and EventReceivers before)
- Well after looking at BuildArbitraryQuery for a while I think I made some sense of it, basically I don't need to change anything? Since it receives the name of the file and the name of the column as a parameter it should be fine right?
Also since items within the list will be something like ClientA Request - 3.docx and I am sending the filename to the BuildArbitraryQuery will be able to find part matches rather than the full match. So for example if the filename BuildArbitraryQuery receives is ClientA Request.docx will it be able to find all other requests from that ClientA? So would ClientA Request - 1.docx, ClientA Request - 2.docx all be included in the calculation?
Here you go, tested it and it works.
/// <summary>
/// An item was added.
/// </summary>
public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);
SPListItem item = properties.ListItem;
if (item["Name"] == null)
return; //or better yet, log
string oldFileName = item["Name"].ToString();
SPQuery query = BuildArbitraryQuery(properties.List, "Created By", properties.UserDisplayName, true);
int count = properties.List.GetItems(query).Count;
int positionOfPeriod = oldFileName.LastIndexOf(".");
if (positionOfPeriod == -1)
{
fileName = oldFileName;
fileExtension = "";
}
else
{
fileName = oldFileName.Substring(0, positionOfPeriod);
fileExtension = oldFileName.Substring(positionOfPeriod);
}
string newFileName = fileName + "-xx" + count.ToString() + fileExtension;
item["Name"] = newFileName;
try
{
properties.Web.AllowUnsafeUpdates = true;
EventFiringEnabled = false;
item.Update();
}
finally
{
properties.Web.AllowUnsafeUpdates = false;
EventFiringEnabled = true;
}
}
/// <summary>
/// Builds an arbitrary SPQuery which filters by a single column value.
/// </summary>
/// <param name="list">The list you will run the query against.</param>
/// <param name="columnDisplayName">The Display Name of the column you want to filter.</param>
/// <param name="value">The value to filter against.</param>
/// <returns>A new SPQuery object ready to run against the list.</returns>
public static SPQuery BuildArbitraryQuery(SPList list, string columnDisplayName, string value, bool deepSearch)
{
if (list == null)
throw new ArgumentNullException("You cannot pass a null list to Helper.BuildArbitraryQuery.");
if (!list.Fields.ContainsField(columnDisplayName))
throw new ArgumentException("The SharePoint List \"" + list.Title + "\" does not contain the Field \"" + columnDisplayName + "\".");
string internalName = list.Fields[columnDisplayName].InternalName;
SPQuery query = new SPQuery();
query.Query = "<Where><Eq><FieldRef Name=\"" + internalName + "\"/><Value Type=\"Text\">" + value + "</Value></Eq></Where>";
if (deepSearch)
query.ViewAttributes += "Scope='RecursiveAll'";
return query;
}

How to list Add items to Sharepoint list using .asmx web service

I have used asmx web service in my project. I want to add items to existing SharePoint list.
mylist.Url = Url.TrimEnd('/') + "/_vti_bin/lists.asmx";
mylist.UseDefaultCredentials = true;
XmlNode node = mylist.GetList(_listName);
And I have store my values in DataTable. How can I directly add data to SharePoint list from C# Datatable?
Or should I convert it to Xml and add ?
thank you
Take a look at this page for the general usage of UpdateListItems, although it only has an example for updating items.
Then look at this page for an example of the XML you need to send to create a new item rather than updating an existing one.
You'll need to loop through your data table constructing an XML structure for each item you want to add, but you can then add them all in a single request.
The following example demonstrates how to consume SharePoint Web Services, in particular Lists Class to create a List Item:
using System;
using System.Collections.Generic;
using System.Net;
using System.Xml;
namespace SharePoint.Client
{
public class ListsClient : IDisposable
{
public ListsClient(Uri webUri, ICredentials credentials)
{
_client = new Lists.Lists();
_client.Credentials = credentials;
_client.Url = webUri + "/_vti_bin/Lists.asmx";
}
public ListsClient(Uri webUri)
{
_client = new Lists.Lists();
_client.Url = webUri + "/_vti_bin/Lists.asmx";
}
/// <summary>
/// Create a List Item
/// </summary>
/// <param name="listName">List Name</param>
/// <param name="propertyValues">List Item properties</param>
/// <returns></returns>
public XmlNode CreateListItem(string listName,Dictionary<string,string> propertyValues)
{
var payload = new XmlDocument();
var updates = payload.CreateElement("Batch");
updates.SetAttribute("OnError", "Continue");
var method = payload.CreateElement("Method");
method.SetAttribute("ID", "1");
method.SetAttribute("Cmd", "New");
foreach (var propertyValue in propertyValues)
{
var field = payload.CreateElement("Field");
field.SetAttribute("Name", propertyValue.Key);
field.InnerText = propertyValue.Value;
method.AppendChild(field);
}
updates.AppendChild(method);
return _client.UpdateListItems(listName, updates);
}
public void Dispose()
{
_client.Dispose();
GC.SuppressFinalize(this);
}
protected Lists.Lists _client; //SharePoint Web Services Lists proxy
}
}
Usage
How to create a Task item:
using (var client = new SPOListsClient(webUrl, userName, password))
{
var taskProperties = new Dictionary<string, string>();
taskProperties["Title"] = "Order approval";
taskProperties["Priority"] = "(2) Normal";
var result = client.CreateListItem(listTitle, taskProperties);
}
References
SharePoint 2007 Web Services
Lists.UpdateListItems Method

Programmatically modifying an embedded resource before registering/referencing it on the page

Firstly, "Modifying" may be the wrong term, I see a few people have posted online just asking whether they can actually modify an embedded resource. What I am wanting to to, is use a resource in my assembly as a kind of template which I would do a find and replace on before registering it on the page - is this possible?
For example; say I have a few lines of jQuery as an embedded resource in my assembly and in this script I am referencing a CSS class name that can be set by the front-end programmer. Since I do not know what the CSS class will be until implementation, is there a way of going through the embedded resource and replacing, say, $myclass$ with ThisClassName.
Any help would be appreciated, if it's not possible then at least tell me so I can stop chasing my tail.
I have solved my little issue by creating an HTTP Handler. In this instance it's called DynamicClientScript.axd.
I have taken a few cuts from my code to give you an idea. The code below gets the standard embedded resource URL and takes the query string from it to add to the path to my handler.
/// <summary>
/// Gets the dynamic web resource URL to reference on the page.
/// </summary>
/// <param name="type">The type of the resource.</param>
/// <param name="resourceName">Name of the resource.</param>
/// <returns>Path to the web resource.</returns>
public string GetScriptResourceUrl(Type type, string resourceName)
{
this.scriptResourceUrl = this.currentPage.ClientScript.GetWebResourceUrl(type, resourceName);
string resourceQueryString = this.scriptResourceUrl.Substring(this.scriptResourceUrl.IndexOf("d="));
DynamicScriptSessionManager sessMngr = new DynamicScriptSessionManager();
Guid paramGuid = sessMngr.StoreScriptParameters(this.Parameters);
return string.Format("/DynamicScriptResource.axd?{0}&paramGuid={1}", resourceQueryString, paramGuid.ToString());
}
/// <summary>
/// Registers the client script include.
/// </summary>
/// <param name="key">The key of the client script include to register.</param>
/// <param name="type">The type of the resource.</param>
/// <param name="resourceName">Name of the resource.</param>
public void RegisterClientScriptInclude(string key, Type type, string resourceName)
{
this.currentPage.ClientScript.RegisterClientScriptInclude(key, this.GetScriptResourceUrl(type, resourceName));
}
The handler then takes its query string to build the URL to the standard resource. Reads the resource and replaces each key with it's value within the dictionary collection (DynamicClientScriptParameters).
The paramGuid is an identifier used to get the correct script parameter collection.
What the handler does...
public void ProcessRequest(HttpContext context)
{
string d = HttpContext.Current.Request.QueryString["d"];
string t = HttpContext.Current.Request.QueryString["t"];
string paramGuid = HttpContext.Current.Request.QueryString["paramGuid"];
string urlFormatter = "http://" + HttpContext.Current.Request.Url.Host + "/WebResource.axd?d={0}&t={1)";
// URL to resource.
string url = string.Format(urlFormatter, d, t);
string strResult = string.Empty;
WebResponse objResponse;
WebRequest objRequest = System.Net.HttpWebRequest.Create(url);
objResponse = objRequest.GetResponse();
using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
{
strResult = sr.ReadToEnd();
// Close and clean up the StreamReader
sr.Close();
}
DynamicScriptSessionManager sessionManager = (DynamicScriptSessionManager)HttpContext.Current.Application["DynamicScriptSessionManager"];
DynamicClientScriptParameters parameters = null;
foreach (var item in sessionManager)
{
Guid guid = new Guid(paramGuid);
if (item.SessionID == guid)
{
parameters = item.DynamicScriptParameters;
}
}
foreach (var item in parameters)
{
strResult = strResult.Replace("$" + item.Key + "$", item.Value);
}
// Display results to a webpage
context.Response.Write(strResult);
}
Then in my code where I want to reference my resource I use the following.
DynamicClientScript dcs = new DynamicClientScript(this.GetType(), "MyNamespace.MyScriptResource.js");
dcs.Parameters.Add("myParam", "myValue");
dcs.RegisterClientScriptInclude("scriptKey");
Then say my script resource contains:
alert('$myParam$');
It will output as if it was:
alert('myValue');
My code also does some caching (using the DynamicScriptSessionManager) but you get the idea...
Cheers
In you codebehind you could read the contents of the embedded resource, switch out whatever you want, and then write the new contents to the response. Something like this:
protected void Page_Load(object sender, EventArgs e)
{
string contents = ReadEmbeddedResource("ClassLibrary1", "ClassLibrary1.TestJavaScript.js");
//replace part of contents
//write new contents to response
Response.Write(String.Format("<script>{0}</script>", contents));
}
private string ReadEmbeddedResource(string assemblyName, string resouceName)
{
var assembly = Assembly.Load(assemblyName);
using (var stream = assembly.GetManifestResourceStream(resouceName))
using(var reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}

Categories